How can I bundle other files when using cx_freeze?

Figured it out. from cx_Freeze import setup,Executable includefiles = [‘README.txt’, ‘CHANGELOG.txt’, ‘helpers\uncompress\unRAR.exe’, , ‘helpers\uncompress\unzip.exe’] includes = [] excludes = [‘Tkinter’] packages = [‘do’,’khh’] setup( name=”myapp”, version = ‘0.1’, description = ‘A general enhancement utility’, author=”lenin”, author_email=”le…@null.com”, options = {‘build_exe’: {‘includes’:includes,’excludes’:excludes,’packages’:packages,’include_files’:includefiles}}, executables = [Executable(‘janitor.py’)] ) Note: include_files must contain “only” relative paths to the setup.py script … Read more

Splitting a semicolon-separated string to a dictionary, in Python

There’s no builtin, but you can accomplish this fairly simply with a generator comprehension: s= “Name1=Value1;Name2=Value2;Name3=Value3” dict(item.split(“=”) for item in s.split(“;”)) [Edit] From your update you indicate you may need to handle quoting. This does complicate things, depending on what the exact format you are looking for is (what quote chars are accepted, what escape … Read more

Subtracting 2 lists in Python

If this is something you end up doing frequently, and with different operations, you should probably create a class to handle cases like this, or better use some library like Numpy. Otherwise, look for list comprehensions used with the zip builtin function: [a_i – b_i for a_i, b_i in zip(a, b)]

Moving x-axis to the top of a plot in matplotlib

Use ax.xaxis.tick_top() to place the tick marks at the top of the image. The command ax.set_xlabel(‘X LABEL’) ax.xaxis.set_label_position(‘top’) affects the label, not the tick marks. import matplotlib.pyplot as plt import numpy as np column_labels = list(‘ABCD’) row_labels = list(‘WXYZ’) data = np.random.rand(4, 4) fig, ax = plt.subplots() heatmap = ax.pcolor(data, cmap=plt.cm.Blues) # put the major … Read more

Load CSV data into MySQL in Python

I think you have to do mydb.commit() all the insert into. Something like this import csv import MySQLdb mydb = MySQLdb.connect(host=”localhost”, user=”root”, passwd=”, db=’mydb’) cursor = mydb.cursor() csv_data = csv.reader(file(‘students.csv’)) for row in csv_data: cursor.execute(‘INSERT INTO testcsv(names, \ classes, mark )’ \ ‘VALUES(“%s”, “%s”, “%s”)’, row) #close the connection to the database. mydb.commit() cursor.close() print … Read more

Attaching a decorator to all functions within a class

The cleanest way to do this, or to do other modifications to a class definition, is to define a metaclass. Alternatively, just apply your decorator at the end of the class definition using inspect: import inspect class Something: def foo(self): pass for name, fn in inspect.getmembers(Something, inspect.isfunction): setattr(Something, name, decorator(fn)) In practice of course you’ll … Read more