Re: 3D visualizations in Python
On Tuesday, March 12, 2019 at 11:54:22 PM UTC+1, Jakub Bista wrote: > Hello. I want to do 3D visualization in Python. Which framework do you > recommend me for creating such a Interface? I would recommend the VTK library (https://vtk.org/). It has excellent Python bindings. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: subprocess : AttributeError: 'Popen' object has no attribute 'read'
On Thursday, January 3, 2019 at 9:40:43 PM UTC+1, Chris Angelico wrote: > On Fri, Jan 4, 2019 at 7:37 AM Mohan Mohta wrote: > > I am no expert in python but I found grep is lot faster in than the methods > > of reading files from python point me to direction if you know of > > anything faster I would appreciate it. > > > > Try doing things the simple and easy way in Python, then figure out if > it's too slow. Only THEN should you worry about "faster". > > ChrisA Yeah, and once you find out you need to go faster and subsequently start looking into ways you *can* go faster, you finally end up with code similar to what is implemented in the grep family of tools. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python-remove duplicate output
On Thursday, November 22, 2018 at 12:10:28 AM UTC+1, drvuc...@gmail.com wrote: > How to remove duplicate lines and store output into one ine > >reservations = ec.describe_instances().get('Reservations', []) > >for reservation in reservations: > for instance in reservation['Instances']: > tags = {} > for tag in instance['Tags']: > tags[tag['Key']] = tag['Value'] > if tag['Key'] == 'Name': > name=tag['Value'] > > if not 'Owner' in tags or tags['Owner']=='unknown' or > tags['Owner']=='Unknown': > print name > > Current Output: > > aws-opsworks > > aws-opsworks Ansible > > Desired output: > > aws-opsworks Ansible You can use a set to do the job. Remember that members in the set are unique, duplicate members are ignored. Before you start the for loop, create a set: names = set() Instead of `print name`, you add the name to the set: names.add(name) Note that if name already exists, because members of a set are unique, the name is (at least conceptually) ignored. Once the for loop is completed, you print the remaining names. print names (Python 2) or print(names) (Python 3) Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: question on the 'calendar' function
On Tuesday, November 20, 2018 at 7:53:06 PM UTC+1, o1bigtenor wrote: > On Tue, Nov 20, 2018 at 11:50 AM Schachner, Joseph > wrote: > > > > It's possible I don't understand the question. The calendar functions are > > NOT limited to this year or any limited range. > > > > Example: > > import calendar > > print( calendar.monthcalendar(2022, 12) ) > > > > Prints lists of dates in each week of December 2022. It prints: > > [[0, 0, 0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, > > 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28, 29, 30, 31, 0]] > > > > So, Dec 1 is a Wednesday; Dec 31 is a Saturday. > > > > That's 49 months ahead of this month. Change the year and month to any > > (valid) number, and it will do what it does. > > The only caveat is that if the moon's orbit slows down as it gets farther > > away from the earth and the earth's rotation speed changes, then the > > calculations done by calendar for leap years may not be correct about the > > distant future. > > > > Greetings > > If my syntax or commands are wrong - - - - I've just started so > something is likely to NOT be correct - - - grin - - - I'sa noob! > > # calendar 2019 > > that is to show the year 2019 > > How could I show June 2018 to Dec 2019, inclusive? > Or June 2018 to Dec 2021, inclusive? > Or June 2018 to Dec 2023 by week (June wk 1,2,3,4 2018; July wk > 1,2,3,4,5 2018; . . . Dec wk 1,2,3,4,5 2023 or maybe even by dates), > inclusive? > > Note that the time frame is ALWAYS more than 1 year. > AIUI there isn't a way to do that, at least not that I can see, and I > would like to be able to do that. > A friend suggested using a script wrapped around the command. I > thought maybe there might we a way of doing what I need to do without > using 2 levels of programming. > > Regards >From what you post it seems like you are on a Linux kind of system and you are >running the `calendar` command in the bash terminal. If that is correct, try >the following: In the bash terminal type (without the literal #): # python This will put you in the Python prompt (marked with >>>). Check that the first line starts with Python 3. If not, I highly recommend you install Python 3. If it is Python two, the following will work, but you have to drop the outer parentheses for print (thus print calendar.calendar()) Then type the following: >>> import calendar >>> print(calendar.calendar(2020)) This will show: 2020 January February March Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 5 1 2 1 6 7 8 9 10 11 12 3 4 5 6 7 8 9 2 3 4 5 6 7 8 13 14 15 16 17 18 19 10 11 12 13 14 15 16 9 10 11 12 13 14 15 20 21 22 23 24 25 26 17 18 19 20 21 22 23 16 17 18 19 20 21 22 27 28 29 30 3124 25 26 27 28 29 23 24 25 26 27 28 29 30 31 April May June Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 5 1 2 3 1 2 3 4 5 6 7 6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14 13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21 20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28 27 28 29 30 25 26 27 28 29 30 31 29 30 July August September Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 5 1 2 1 2 3 4 5 6 6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13 13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20 20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27 27 28 29 30 3124 25 26 27 28 29 30 28 29 30 31 October November December Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 1 1 2 3 4 5 6 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31 30 Now look at the documentation of the calendar module to find out about other options. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there are good DRY fix for this painful design pattern?
On Monday, February 26, 2018 at 3:44:14 PM UTC+1, Steven D'Aprano wrote: > I have a class with a large number of parameters (about ten) assigned in > `__init__`. The class then has a number of methods which accept > *optional* arguments with the same names as the constructor/initialiser > parameters. If those arguments are None, the defaults are taken from the > instance attributes. > > An example might be something like this: > > > class Foo: > def __init__(self, bashful, doc, dopey, grumpy, >happy, sleepy, sneezy): > self.bashful = bashful # etc > > def spam(self, bashful=None, doc=None, dopey=None, >grumpy=None, happy=None, sleepy=None, >sneezy=None): > if bashful is None: > bashful = self.bashful > if doc is None: > doc = self.doc > if dopey is None: > dopey = self.dopey > if grumpy is None: > grumpy = self.grumpy > if happy is None: > happy = self.happy > if sleepy is None: > sleepy = self.sleepy > if sneezy is None: > sneezy = self.sneezy > # now do the real work... > > def eggs(self, bashful=None, # etc... >): > if bashful is None: > bashful = self.bashful > # and so on > > > There's a lot of tedious boilerplate repetition in this, and to add > insult to injury the class is still under active development with an > unstable API, so every time I change one of the parameters, or add a new > one, I have to change it in over a dozen places. > > Is there a good fix for this to reduce the amount of boilerplate? > > > Thanks, > > > > -- > Steve What about something along these lines: from inspect import getargspec, getargvalues class Demo(object): def __init__(self, a=None, b=10, c='oops'): spec = getargspec(self.__init__) for key, value in zip(spec.args[1:], spec.defaults): setattr(self, key, value) def foo(self, *args, **kwargs): assert len(args) == 0 for k, v in kwargs.items(): try: getattr(self, k) except AttributeError: print('*** error: attribute {} does not exist.'.format(k)) setattr(self, k, v) d = Demo() print(d.a) d.foo(a=3.14) print(d.a) d.foo(q='this will raise') So, use the inspect module to detect the valid arguments from the class initializer. Then use **kwargs in every class method. It would be nice if the full method signature can be kept, but I have not yet figured out if this is possible. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Wednesday, March 15, 2017 at 11:33:56 AM UTC+1, Peter Otten wrote: > marco.naw...@colosso.nl wrote: > > > Dear All, > > > > Summary of the question: > > Is it generally safe to dynamically change an objects class; if not > > under which conditions can it be considered safe. > > > > Context: > > Given the code below, I have no direct control over Base and M1. M1 > > is a instantiated by 'calling' the read-only property of Base. > > I would like to transparently switch behaviour from M1 to M2. I have > > tried several things, but finally settled on the code below. Although > > it works really well, I am not sure whether I am about to shoot myself > > in the foot. In short, what I do is derive from Base, shadow the read-only > > property 'my_prop' so it returns M2 and finally replace an objects > > __class__ attribute with my derived class. > > > > Any thoughts or comments? > > Saying that something is "safe" is always problematic. I'd rather look for > ways to break the code and then decide if you can live with these potential > problems. I'll start with > > - If Base is reimplemented in C __class__ may become read-only > - my_prop may be accessed by Base methods and expect an M1 instance Ok. These are valid points. The first is not a real concern to me at the moment. The whole purpose of Base is to provide a higher level interface to the underlying C/C++ code. The second is more interesting. I was aware of this and checked the source code of the library. I know an M1 instance is not directly used by Base. In addition, I expose the exact same interface in M2. Thanks for the feedback. Much appreciated! Marco -- https://mail.python.org/mailman/listinfo/python-list
Dynamically replacing an objects __class__; is it safe?
Dear All, Summary of the question: Is it generally safe to dynamically change an objects class; if not under which conditions can it be considered safe. Context: Given the code below, I have no direct control over Base and M1. M1 is a instantiated by 'calling' the read-only property of Base. I would like to transparently switch behaviour from M1 to M2. I have tried several things, but finally settled on the code below. Although it works really well, I am not sure whether I am about to shoot myself in the foot. In short, what I do is derive from Base, shadow the read-only property 'my_prop' so it returns M2 and finally replace an objects __class__ attribute with my derived class. Any thoughts or comments? (code below is Python 3) class M1(object): def __init__(self): print('Initializing M1') class M2(object): def __init__(self): print('Initializing M2') class Base(object): @property def my_prop(self): return M1() class ShadowBase(Base): @property def my_prop(self): return M2() if __name__ == '__main__': o = Base() o.my_prop # Prints 'Initializing M1' o = Base() o.__class__ = ShadowBase o.my_prop # Prints 'Initializing M2' -- https://mail.python.org/mailman/listinfo/python-list
Re: best way to read a huge ascii file.
On Tuesday, November 29, 2016 at 3:18:29 PM UTC+1, Heli wrote: > Hi all, > > Let me update my question, I have an ascii file(7G) which has around 100M > lines. I read this file using : > > f=np.loadtxt(os.path.join(dir,myfile),delimiter=None,skiprows=0) > > x=f[:,1] > y=f[:,2] > z=f[:,3] > id=f[:,0] > > I will need the x,y,z and id arrays later for interpolations. The problem is > reading the file takes around 80 min while the interpolation only takes 15 > mins. > > I tried to get the memory increment used by each line of the script using > python memory_profiler module. > > The following line which reads the entire 7.4 GB file increments the memory > usage by 3206.898 MiB (3.36 GB). First question is Why it does not increment > the memory usage by 7.4 GB? > > f=np.loadtxt(os.path.join(dir,myfile),delimiter=None,skiprows=0) > > The following 4 lines do not increment the memory at all. > x=f[:,1] > y=f[:,2] > z=f[:,3] > id=f[:,0] > > Finally I still would appreciate if you could recommend me what is the most > optimized way to read/write to files in python? are numpy np.loadtxt and > np.savetxt the best? > > Thanks in Advance, Hi, Have you considered storing the data in HDF5? There is an excellent Python interface for this (see: http://www.h5py.org/). The advantage that you will have is that no text to number conversion has to applied anymore. You can directly operate on the datasets in the HDF5 database. If you would go this direction, the following would get you started: >>> import h5py >>> import numpy >>> from numpy import uint32, float32, arange >>> fd = h5py.File('demo.h5', mode='w') # Note that this will truncate # the file, use 'r' or 'a' if you # want to open an existing file >>> observations = fd.create_group('/observations') >>> N = 100 >>> observations.create_dataset('id', data=arange(0, N, dtype=uint32)) >>> observations.create_dataset('x', data=numpy.random.random(N), dtype=float32) >>> observations.create_dataset('y', data=numpy.random.random(N), dtype=float32) >>> observations.create_dataset('z', data=numpy.random.random(N), dtype=float32) >>> >>> fd.close() Note that you can also combine x,y and z in a single dataset if you want to. See the documentation for datasets for more information http://docs.h5py.org/en/latest/high/dataset.html I would also advise you to carefully select the proper dtype for the arrays. In particular if you know the value range for your datasets. This can save you a lot of disk space and probably will increase the performance a little. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: data interpolation
On Thursday, November 3, 2016 at 11:08:34 AM UTC+1, Heli wrote: > Hi, > > I have a question about data interpolation using python. I have a big ascii > file containg data in the following format and around 200M points. > > id, xcoordinate, ycoordinate, zcoordinate > > then I have a second file containing data in the following format, ( 2M > values) > > id, xcoordinate, ycoordinate, zcoordinate, value1, value2, value3,..., valueN > > I would need to get values for x,y,z coordinates of file 1 from values of > file2. > > I don´t know whether my data in file1 and 2 is from structured or > unstructured grid source. I was wondering which interpolation module either > from scipy or scikit-learn you recommend me to use? > > I would also appreciate if you could recommend me some sample > example/reference. > > Thanks in Advance for your help, Take a look at the scipy.spatial.KDTree class: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.KDTree.html Given your example, you would build the tree (using the coordinates) from the second file. Subsequently, you can use one of the query methods for every point in your first file. From this it is up to you how to transfer (interpolate) the values. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: get the sum of differences between integers in a list
On Wednesday, September 21, 2016 at 4:14:10 PM UTC+2, Daiyue Weng wrote: > Hi, first of all, let me rephase the problem. > > For an arbitrary list of integers (the integers in the list are not > necessary to be sequential), e.g. [1,2,3,6,8,9,10,11,13], > > if a set of consecutive integers having a difference of 1 between them, put > them in a list, i.e. there are two such lists in this example, > > [1,2,3], > > [8,9,10,11], > > and then put such lists in another list (i.e. [[1,2,3], [8,9,10,11]]). Put > the rest integers (non-sequential) in a separated list, i.e. > > `[6, 13]`. > > Note that, the problem only considers sequential integers with step_size = > 1. > > I tried to use itertools.groupby and itertools.count, > > from itertools import groupby, count > lst = [1,2,3,6,8,9,10,11,13] > c = count() > result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c))] > > but the result is close to the requirement shown as a list of lists, > > [[1, 2, 3], [6], [8, 9, 10, 11], [13]] > > but it didn't separate sequential lists from non-sequential ones. > Also, I couldn't find a way to put [6] and [13] in a single list. > > I have also tried to separate sequential lists from non-sequential ones, > > result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) == 1)] # > tried to extract [1,2,3] and [8,9,10,11] from the list > > or > > result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) > 1)] # > tried to extract [6] and [13] from the list > > but they all ended up with > > [[1, 2, 3], [6, 8, 9, 10, 11, 13]] > > So two questions here, > > 1. How does itertools.groupby key function work in my case? > > 2. How to improve the program to achieve my goals? > > > Many thanks > > On 21 September 2016 at 00:19, John Potewrote: > > > On 20/09/2016 12:52, Daiyue Weng wrote: > > > > Hi, I have a list numbers say, > >> > >> [1,2,3,4,6,8,9,10,11] > >> > >> First, I want to calculate the sum of the differences between the numbers > >> in the list. > >> > > At least for this first part a little pencil, paper and algebra yields a > > simple formula of constant and minimal calculation time. I had an intuitive > > guess and wrote down the sum of differences for a couple of examples, > > [1, 2, 5] => 4 > > [9, 11, 17, 19] => 10 > > It works for negative differences as well, > > [1, 2, 5, 1]=> 0 > > The trick is to spot the relation between the sum of differences and the > > numbers in the list. A few lines of algebra then gave a very simple formula. > > > > As for the rest it's down to code as others have hinted at. > > > > Second, if a sequence of numbers having a difference of 1, put them in a > >> list, i.e. there are two such lists, > >> > >> [1,2,3] > >> > >> [8,9,10,11] > >> > >> and also put the rest numbers in another list, i.e. there is only one such > >> list in the example, > >> > >> [6]. > >> > >> Third, get the lists with the max/min sizes from above, i.e. in this > >> example, the max list is, > >> > >> [8,9,10,11] > >> > >> min list is > >> > >> [1,2,3]. > >> > >> What's the best way to implement this? > >> > >> cheers > >> > > > > -- > > https://mail.python.org/mailman/listinfo/python-list > > On Wednesday, September 21, 2016 at 4:14:10 PM UTC+2, Daiyue Weng wrote: > Hi, first of all, let me rephase the problem. > > For an arbitrary list of integers (the integers in the list are not > necessary to be sequential), e.g. [1,2,3,6,8,9,10,11,13], > > if a set of consecutive integers having a difference of 1 between them, put > them in a list, i.e. there are two such lists in this example, > > [1,2,3], > > [8,9,10,11], > > and then put such lists in another list (i.e. [[1,2,3], [8,9,10,11]]). Put > the rest integers (non-sequential) in a separated list, i.e. > > `[6, 13]`. > > Note that, the problem only considers sequential integers with step_size = > 1. > > I tried to use itertools.groupby and itertools.count, > > from itertools import groupby, count > lst = [1,2,3,6,8,9,10,11,13] > c = count() > result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c))] > > but the result is close to the requirement shown as a list of lists, > > [[1, 2, 3], [6], [8, 9, 10, 11], [13]] > > but it didn't separate sequential lists from non-sequential ones. > Also, I couldn't find a way to put [6] and [13] in a single list. > > I have also tried to separate sequential lists from non-sequential ones, > > result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) == 1)] # > tried to extract [1,2,3] and [8,9,10,11] from the list > > or > > result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c) > 1)] # > tried to extract [6] and [13] from the list > > but they all ended up with > > [[1, 2, 3], [6, 8, 9, 10, 11, 13]] > > So two questions here, > > 1. How does itertools.groupby key function work in my case? > > 2. How to improve the program to achieve my goals? > > > Many thanks > > On 21 September
Re: passing dictionay as argument
On Monday, June 13, 2016 at 12:54:45 PM UTC+2, Arshpreet Singh wrote: > I have to pass dictionary as function argument for following code: > > > import authorize > > authorize.Configuration.configure( > authorize.Environment.TEST, > 'api_login_id', > 'api_transaction_key', > ) > > result = authorize.Transaction.sale({ > 'amount': 40.00, > > 'credit_card': { > 'card_number': '4111', > 'expiration_date': '04/2014', > 'card_code': '343', > } > > }) > > result.transaction_response.trans_id > > > > I want to define 'credit-card' dictionary as argument in the function as > follows but it returns syntax error: > > > > # define dictionary outside the function call: > credit_card={ > 'card_number': '4111', > 'expiration_date': '04/2014', > 'card_code': '343', > } > > import authorize > > authorize.Configuration.configure( > authorize.Environment.TEST, > 'api_login_id', > 'api_transaction_key', > ) > > result = authorize.Transaction.sale({'amount': 40.00,credit_card}) > > result.transaction_response.trans_id > > it returns following error: > > result = authorize.Transaction.sale({40.00,credit_card}) > TypeError: unhashable type: 'dict' > > Do I need to make changes in authorize.Transaction.sale() source code? You explicitly need to specify the key for credit_card in the call to sale(..). I have not run the code myself, but I believe it will work like this: result = authorize.Transaction.sale({'amount': 40.00,'credit_card': credit_card}) -- https://mail.python.org/mailman/listinfo/python-list
Re: More list building
On Wednesday, May 11, 2016 at 7:22:09 PM UTC+2, DFS wrote: > Have: > p1 = ['Now', 'the', 'for', 'good'] > p2 = ['is', 'time', 'all', 'men'] > > want > [('Now','is','the','time'), ('for','all','good','men')] > > This works: > > p = [] > for i in xrange(0,len(p1),2): > p.insert(i,(p1[i],p2[i],p1[i+1],p2[i+1])) > > > But it seems clunky. > > Better way(s)? > > Thanks Another way using some array manipulation: (using Python 2.7 and numpy) # coding: utf-8 import numpy as np p1 = np.array(('Now', 'the', 'for', 'good')) p2 = np.array(('is', 'time', 'all', 'men')) p1s = np.split(p1, 2) p2s = np.split(p2, 2) a1 = np.vstack((p1, p2)) r1 = np.hstack((a1[:, 0], a1[:, 1])) r2 = np.hstack((a1[:, 2], a1[:, 3])) print r1 print r2 Produces the following output: ['Now' 'is' 'the' 'time'] ['for' 'all' 'good' 'men'] -- https://mail.python.org/mailman/listinfo/python-list
Re: Phyton
On Sunday, March 6, 2016 at 6:10:22 PM UTC+1, Mark Lawrence wrote: > On 06/03/2016 15:28, marco.naw...@colosso.nl wrote: > > On Sunday, March 6, 2016 at 3:16:19 PM UTC+1, Diego ... wrote: > >> Hello! I have a question in an exercise that says : Write an expression to > >> determine whether a person should or should not pay tax . Consider paying > >> tax people whose salary is greater than R $ 1,200.00 > >> > >> I do not know how to mount the logical expression !!! > >> > >> It's like: > >> > >> salary = 1250 > >> tax = Not True > >> salary > 1200 or not tax > > > > Hello Diego, > > > > You are looking for the "if" statement. See the link below for > > the corresponding documentation: > > https://docs.python.org/2/tutorial/controlflow.html > > > > Your example would become something like: > > > > salary = 1250. > > if salary > 1200: > > has_to_pay_tax = True > > else: > > has_to_pay_tax = False > > > > Marco > > > > Why in the year 2016 are people still giving links to the Luddite Python > 2 docs? > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence On Sunday, March 6, 2016 at 6:10:22 PM UTC+1, Mark Lawrence wrote: > On 06/03/2016 15:28, marco.naw...@colosso.nl wrote: > > On Sunday, March 6, 2016 at 3:16:19 PM UTC+1, Diego ... wrote: > >> Hello! I have a question in an exercise that says : Write an expression to > >> determine whether a person should or should not pay tax . Consider paying > >> tax people whose salary is greater than R $ 1,200.00 > >> > >> I do not know how to mount the logical expression !!! > >> > >> It's like: > >> > >> salary = 1250 > >> tax = Not True > >> salary > 1200 or not tax > > > > Hello Diego, > > > > You are looking for the "if" statement. See the link below for > > the corresponding documentation: > > https://docs.python.org/2/tutorial/controlflow.html > > > > Your example would become something like: > > > > salary = 1250. > > if salary > 1200: > > has_to_pay_tax = True > > else: > > has_to_pay_tax = False > > > > Marco > > > > Why in the year 2016 are people still giving links to the Luddite Python > 2 docs? > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > Why in the year 2016 are people still giving links to the Luddite Python >2 docs? As Ian already mentioned, the Python 2 docs came up as the first hit on Google. However, I agree with you that for a newcomers to Python a link to Python 3 would probably have been more appropriate (not that I believe the content of the sections would be any different). As a side note, you are probably aware that if you look at the Linux ecosystems there are still a lot of distributions that have Python 2 as a default. There are still also large mainstream libraries that do not (or just very recently) have support for Python 3. For me this in particular applied to VTK. I am now finally ready to move to Python 3. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Phyton
On Sunday, March 6, 2016 at 3:16:19 PM UTC+1, Diego ... wrote: > Hello! I have a question in an exercise that says : Write an expression to > determine whether a person should or should not pay tax . Consider paying tax > people whose salary is greater than R $ 1,200.00 > > I do not know how to mount the logical expression !!! > > It's like: > > salary = 1250 > tax = Not True > salary > 1200 or not tax Hello Diego, You are looking for the "if" statement. See the link below for the corresponding documentation: https://docs.python.org/2/tutorial/controlflow.html Your example would become something like: salary = 1250. if salary > 1200: has_to_pay_tax = True else: has_to_pay_tax = False Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Cycling through iterables diagonally
On Friday, February 26, 2016 at 8:44:38 AM UTC+1, Pablo Lucena wrote: > Hello, > > I am trying to accomplish the following: > > Say I have a group of 4 lists as follows: > > l1 = ['a1', 'a2', 'a3', 'a4'] > l2 = ['b1', 'b2', 'b3', 'b4'] > l3 = ['c1', 'c2', 'c3', 'c4'] > l4 = ['d1', 'd2', 'd3', 'd4'] > > I would like to cycle through these lists "diagonally" in groups of > len(list) (in this example, each list has 4 items). > > cycle1: a1, b2, b3, b4 > cycle2: a2, b3, c4, d1 > cycle3: a3, b4, c1, d2 > cycle4: a4, b1, c2, d3 > > The way I thought about doing this is as follows: > > from collections import deque > from itertools import cycle > > l1 = deque(['a1', 'a2', 'a3', 'a4']) > l2 = deque(['b1', 'b2', 'b3', 'b4']) > l3 = deque(['c1', 'c2', 'c3', 'c4']) > l4 = deque(['d1', 'd2', 'd3', 'd4']) > > l1.rotate(-0) > l2.rotate(-1) > l3.rotate(-2) > l4.rotate(-3) > > groups = cycle([l1, l2, l3, l4]) > > In [115]: for group in groups: >.: if not group: >.: break >.: print(group.popleft()) >.: > a1 > b2 > c3 > d4 > a2 > b3 > c4 > d1 > a3 > b4 > c1 > d2 > a4 > b1 > c2 > d3 > > Prior to this I was mucking around with index counting while looping, and > popping lists out of a deque, popping an item out of the list, and > appending the list back into the deque during each iteration. > > Is there a better/cleaner way to do this? I was hoping for some cool > itertools logic =) > > Thanks! > > > -- > *Pablo* Hello Pablo, If you don't mind using third-party packages you could this quite straighforward with numpy: (Python 2.7) import numpy as np a = np.array([ ('a1', 'a2', 'a3', 'a4'), ('b1', 'b2', 'b3', 'b4'), ('c1', 'c2', 'c3', 'c4'), ('d1', 'd2', 'd3', 'd4')], dtype=str) nrows, ncols = a.shape for i in range(ncols): print np.diag(np.roll(a, -i, axis=1)) This prints: ['a1' 'b2' 'c3' 'd4'] ['a2' 'b3' 'c4' 'd1'] ['a3' 'b4' 'c1' 'd2'] ['a4' 'b1' 'c2' 'd3'] -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] how to create log files
Hello Jens, Are you aware of Python's own logging facility? It is quite powerful and flexible. Python 2: https://docs.python.org/2/library/logging.html Python 3: https://docs.python.org/3/library/logging.html Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Using tuple as parameter to a function
On Monday, November 9, 2015 at 2:58:21 PM UTC+1, Chris Angelico wrote: > On Tue, Nov 10, 2015 at 12:40 AM, Cecil Westerhofwrote: > > I was thinking about something like: > > values = (( 1, 100), ( 2, 100), ( 5, 100), > >10, 100), (20, 100), (40, 100)) > > for value in values: > > do_stress_test('sqlite', ???) > > do_stress_test('postgres', ???) > > > > Is this possible? If so: what do I put at the place of the '???'? > > > > I could change the second and third parameter to a tuple as the second > > parameter, but I prefer three parameters if that would be possible. > > Easy! Just unpack the tuple. Two options: > > # Unpack in the loop > for count, size in values: > do_stress_test('sqlite', count, size) > do_stress_test('postgres', count, size) > > # Unpack in the function call > for value in values: > do_stress_test('sqlite', *value) > do_stress_test('postgres', *value) > > Either will work. For what you're doing here, I'd be inclined to the > first option, so you can give the values appropriate names (I'm > completely guessing here that they might be some sort of iteration > count and pool size; use names that make sense to your program); the > other option looks uglier in this particular instance, though it's a > more direct answer to your question. > > This is one of Python's best-kept secrets, I think. It's not easy to > stumble on it, but it's so handy once you know about it. > > ChrisA If the two numbers are actually conceptually connected, you could consider passing them as a namedtuple. The function would then receive two parameters instead of three. Something like the following (expanding on ChrisA's example): from collections import namedtuple StressParameters = namedtuple('StressParameters', ('count', 'size')) def do_stress_test(db_backend, stress_parameters): # Some useful stuff... count = stress_parameters.count size = stress_parameters.size # More useful stuff parameters = [StressParameters(1, 100), StressParameters(2,100)] for p in parameters: do_stress_test('sqlite', p) do_stress_test('postgres', p) -- https://mail.python.org/mailman/listinfo/python-list
Re: Python PNG Viewer(Browser Based)
On Tuesday, November 3, 2015 at 12:55:09 PM UTC+1, Arshpreet Singh wrote: > Hello Everyone, > > I am looking for Browser-based PNG file viewer written in > Python.(Flask framework preferably) > > Following project(Flask-Based) provides many things(File manager as > well as file viewer) but it does not support PNG files. > > https://github.com/vmi356/filemanager > > Any idea if I have to write my own browser based PNG viewer from > scratch(Using PIL or other required library) > > On the other side if I have to write only Desktop-based only two lines > are enough to do many things: > > Like, > > from PIL import Image > f = Image.open("file.png").show() > > > But I am not getting right sense that how to make possible using Python+Flask. > > Hope I am able to tell my problem? Well, if you only want to open the PNG file in the webbrowser (in contrast of serving them over the internet), you can do it in two lines too: import webbrowser webbrowser.open('file.png') But this is probably not what you want. M. -- https://mail.python.org/mailman/listinfo/python-list
Re: Python, convert an integer into an index?
On Wednesday, September 23, 2015 at 1:27:51 AM UTC+2, MRAB wrote: > On 2015-09-22 23:21, Laura Creighton wrote: > > In a message of Tue, 22 Sep 2015 14:43:55 -0700, Chris Roberts writes: > >> > >> > >>(How do I make it into an index? ) > >>Preferably something fairly easy to understand as I am new at this. > >> > >>results = 134523 #(Integer) > >> > >>Desired: > >>results = [1, 2, 3, 4, 5, 2, 3] #(INDEX) > >> > >>Somehow I see ways to convert index to list to int, but not back again. > >> > >>Thanks, > >>crzzy1 > > > > You need to convert your results into a string first. > > > > result_int=1234523 > > result_list=[] > > > > for digit in str(result_int): > > result_list.append(int(digit)) > > > > digit will be assigned to successive 1 character long strings. Since > > you wanted a list of integers, you have to convert it back. > > > > If you are learning python you may be interested in the tutor mailing > > list. https://mail.python.org/mailman/listinfo/tutor > > > A shorter way using strings: > > >>> results = 134523 > >>> list(map(int, str(results))) > [1, 3, 4, 5, 2, 3] Or you can use a list comprehension: >>> result = [int(c) for c in str(134523)] >>> print result [1, 3, 4, 5, 2, 3] -- https://mail.python.org/mailman/listinfo/python-list
Re: Packaging and deployment of standalone Python applications?
On Monday, September 14, 2015 at 8:58:51 AM UTC+2, Kristian Rink wrote: > Folks; > > coming from a server-sided Java background, I'm recently exploring frameworks > such as cherrypy or webpy for building RESTful services, which is quite a > breeze and a pretty pleasant experience; however one thing so far bugs me: > Using Java tooling and libraries such as DropWizard, it is pretty > straightforward to build an "all-inclusive" application containing (a) all > code of my own, (b) all required dependencies, (c) all other resources and, > if required, even (d) a Java virtual machine in one large .zip file which can > easily be copied to a clean Linux VM, unzipped and started there. > > Are there ways of doing so using Python, too? I'd like to set up a project > structure / working environment that includes all Python 3rd party libraries, > my own code and maybe even a "standard" Python runtime for 64bit Linux > systems (to not depend too much on what version a certain Linux distribution > actually ships) and focus on doing deployment to various machines at best by > simply copying these packages around. > > Any pointers, ideas, inspirations on that greatly appreciated - even in total > different ways if what I am about to do is completely off anyone would do it > in a Python environment. ;) > > TIA and all the best, > Kristian If your business-cases allows it, I would seriously consider using Docker. I makes it pretty straightforward to move your deployments around from your development machine, to a test setup, to a cloud provider (e.g. AWS) etc. Lack or incomplete support on Windows systems is a little bit a deal breaker, but this situation is improving quickly. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: From logging to files to a better solution: syslog, Sentry, Logstash, ....
On Friday, September 11, 2015 at 9:22:42 AM UTC+2, Thomas Güttler wrote: > Am Donnerstag, 10. September 2015 08:42:47 UTC+2 schrieb dieter: > > Thomas Güttler writes: > > > ... > > > Why we are unhappy with logging to files: > > > > > > - filtering: We don't want to get INFO messages over the VPN. > > > > You can quite easily control at what level messages are logged with > > the standard Python logging framework. Each handler has a level > > and will ignore messages at a lower level. > > > I want INFO to be logged and stored on the remote host. > Therefore I must not filter INFO messages. > > I don't want to pull INFO messages over the VPN. > > Ergo, the filtering at Python level does not help in my use case. > Or I am missing something. > > And now I have an ugly soup. > > The ugly soup is a text file with not well defined syntax. It looks line > based. But sometimes there are log messages which span multiple lines > > Remember: "Life is too short to (re)write parsers" > > Yes, there are tools to parse that soup. But why create this soup in > the first place? > > That's why I want to move from file based logging to a different solution. > > Unfortunately there too many choices (graylog, logstash, sentry, ...) :-( > > > > > > - Rotating: Rotating files is possible, but somehow cumbersome. > > > > There are standard tools to rotate logfiles. > > > > > - Support structured logging of values (json) in the future. > > > > Again, the Python logging framework is quite flexible with > > respect to the format of logged messages. > > > > > ... > > > Which solution could fit for our environment? > > > > I work for a customer with a similar environment (he uses "Zope" instead > > of "Django") - and he uses logfiles. The logfiles are automatically > > rotated and there are in the order of half a dozen to a dozen logfiles > > per day. > > > > When I have to analyse a problem with the help of the logfiles, > > I do not copy them via VPN but do the filtering remotely and only > > copy the filtered portion, if necessary. > > Good to know that I am not the only one running servers in remote intranets. > > Regards, > Thomas Güttler So, if logging to json is on the horizon anyway, why don't you create something like a MongoDb handler in the standard Python logging framework and run a MongoDb server in the client intranet? You could then connect over VPN to MongoDb, filter the warning/error messages there on the server side and fetch them to your local systems for analysis. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Most Pythonic way to store (small) configuration
On Sunday, August 2, 2015 at 12:14:51 PM UTC+2, Cecil Westerhof wrote: There are a lot of ways to store configuration information: - conf file - xml file - database - json file - and possible a lot of other ways I want to write a Python program to display cleaned log files. I do not think I need a lot of configuration to be stored: - some things relating to the GUI - default behaviour - default directory - log files to display, including some info - At least until where it was displayed Because of this I think a human readable file would be best. Personally I do not find XML very readable. So a conf or json file looks the most promising to me. And I would have a slight preference for a json file. Any comments, thoughts or tips? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof Why not use Python files itself as configuration files? I have done so several times and I am very pleased with it. I have tried both INI and JSON, but in the end always settled on using Python itself. If I have to share the configuration information with other languages (like C++ or Javascript) I use JSON as the For those who are interested, I provide a short summary below on how I handle configuration files. The scenario I want to support is that there is a hierarchical set of configuration files (as typically found in Unix like systems) that can be stored in a system wide folder (like /etc/app), in a user folder (like /home/user/.config/app) and in a local folder. Each configuration file has a fixed name, not necessarily ending in .py. So for example, the name of the configuration file is config.myapp. Next I start looking for the existence of these files and once I find them I start merging all the configuration settings into one Python dictionary. The function that I use can be found below. (you need proper import statements, but this is standard Python). def load_config(config_file, checked=False): '''Small utility function to load external configuration files. ''' if checked is False: if not isfile(config_file) or islink(config_file): msg = 'Config file %s does not exist.' LOGGER.warning(msg) return {} # Load the module object; we use the imp.load_source function because # the module configuration typically has a non-standard file extension # (by default the file is called config.myapp) module_name = 'm%s' % str(uuid.uuid1()).replace('-', '') # Prevent bytecode generation for configuration files sys.dont_write_bytecode = True config_module = imp.load_source(module_name, config_file) # Continue with bytecode generation for other normal modules sys.dont_write_bytecode = False if not hasattr(config_module, '__config__'): msg = 'Missing __config__ attribute in configuration file.' LOGGER.warning(msg) return {} else: return config_module.__config__ -- https://mail.python.org/mailman/listinfo/python-list
Re: How to import a function from another module...
On Tuesday, August 4, 2015 at 3:11:41 PM UTC+2, Dwight GoldWinde wrote: Thank you, Steven. I am a newbie with Python? so I really want to learn how to do it the easy way. Yes, could you tell me how to put the py.file that contains the function in the Python search path??? BIG SMILE... Always, Dwight www.3forliving.key.to (video playlist on YouTube) www.couragebooks.key.to (all my books on Amazon) On 8/4/15, 9:24 AM, Steven D'Aprano st...@pearwood.info wrote: On Tue, 4 Aug 2015 09:57 am, Dwight GoldWinde wrote: I am trying to import a function defined in another module. You can't use spaces in the name of importable Python modules: change the name from Simulate typing.py to simulate_python.py. You can use spaces in file names if they are only used as a runnable script and not imported. Then use this: from simulate_python import humprint There's no need to use importlib. You may need to arrange for the simulate_python file to be placed somewhere in the Python search path. Do you need help with that? What you are trying to do with importlib is fight the language. Your life will be much simpler if you work within the parameters of how the language is designed to work. -- Steven -- https://mail.python.org/mailman/listinfo/python-list Hi Dwight, There are at least two ways to manipulate your search path, one from within Python, one by setting/updating an environmental variable in your operating system. 1. Dynamically add a search path in Python First note that if you want to know which parts are currently in the Python search path you can do the following (note that means type it in the Python shell and I am using Python 2 syntax) import sys for path in sys.path: ... print path So, if you want to add a folder to your search path, in your script add the following *before* you access attributes etc. from the module your are trying to use: sys.path.append(the_path_that_contains_the_py_file) # Import stuff from the py file here The downside of this approach is that you have to do this in every script that accesses information from your .py file. If you want to do this once and for all, use the second method 2. Setting/updating the environment variable You can use the environment variable PYTHONPATH to inform Python on which search paths it should use (besides the default ones). -- https://mail.python.org/mailman/listinfo/python-list
Re: Most Pythonic way to store (small) configuration
On Tuesday, August 4, 2015 at 7:06:33 PM UTC+2, Irmen de Jong wrote: On 4-8-2015 16:53, marco.naw...@colosso.nl wrote: Why not use Python files itself as configuration files? It could create a security risk if the config files are user-editable. (it will make it easy to inject code into your application) Irmen Yes, I am aware of the security risk, but right now I am not too worried about it. To be honest, JSON would be my preferred configuration file format, but as others already mentioned, there is no standard way of adding comments to a JSON file. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Creating a reliable sandboxed Python environment
On Tuesday, May 26, 2015 at 4:24:32 AM UTC+2, davi...@gmail.com wrote: I am writing a web service that accepts Python programs as input, runs the provided program with some profiling hooks, and returns various information about the program's runtime behavior. To do this in a safe manner, I need to be able to create a sandbox that restricts what the submitted Python program can do on the web server. Almost all discussion about Python sandboxes I have seen on the internet involves selectively blacklisting functionality that gives access to system resources, such as trying to hide the open builtin to restrict access to file I/O. All such approaches are doomed to fail because you can always find a way around a blacklist. For my particular sandbox, I wish to allow *only* the following kinds of actions (in a whitelist): * reading from stdin writing to stdout; * reading from files, within a set of whitelisted directories; * pure Python computation. In particular all other operations available through system calls are banned. This includes, but is not limited to: * writing to files; * manipulating network sockets; * communicating with other processes. I believe it is not possible to limit such operations at the Python level. The best you could do is try replacing all the standard library modules, but that is again just a blacklist - it won't prevent a determined attacker from doing things like constructing their own 'code' object and executing it. It might be necessary to isolate the Python process at the operating system level. * A chroot jail on Linux OS X can limit access to the filesystem. Again this is just a blacklist. * No obvious way to block socket creation. Again this would be just a blacklist. * No obvious way to detect unapproved system calls and block them. In the limit, I could dynamically spin up a virtual machine and execute the Python program in the machine. However that's extremely expensive in computational time. Has anyone on this list attempted to sandbox Python programs in a serious fashion? I'd be interested to hear your approach. - David What about launching the Python process in a Docker container? Spinning up a new container is pretty quick and it might provide you with enough isolation. Probably not a perfect solution, but I do believe that it would be easier than trying to sandbox Python itself. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Could not find any downloads that satisfy the requirement novaclient
On Tuesday, April 7, 2015 at 9:55:58 PM UTC+2, kurt_...@symantec40.com wrote: Hi- While trying to install an OpenStack client on Mac OSX, I get the following: SymMacToolkit-C02N4H9DG3QD:/ kurt_heiss$ sudo pip install novaclient Password: The directory '/Users/kurt_heiss/Library/Logs/pip' or its parent directory is not owned by the current user and the debug log has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. The directory '/Users/kurt_heiss/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. The directory '/Users/kurt_heiss/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag. Collecting novaclient Could not find any downloads that satisfy the requirement novaclient No distributions at all found for novaclient SymMacToolkit-C02N4H9DG3QD:/ kurt_heiss$ I changed the permissions on the identified folders as well as tried the -H option to no positive result. Anybody encounter this issue or suggest a possible workaround? Hi, The module you are looking for is named python-novaclient and not novaclient. You can check for yourself by running pip search novaclient. -- https://mail.python.org/mailman/listinfo/python-list
Re: A question about how plot from matplotlib works
On Thursday, February 19, 2015 at 11:47:53 AM UTC+1, ast wrote: Hello import numpy as np import matplotlib.pyplot as plt x = np.arange(10) y = x**2 x array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) y array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81]) plt.plot(x,y) [matplotlib.lines.Line2D object at 0x044F5930] plt.show() The question is: plt.plot() creates an object matplotlib.lines.Line2D but this object is not referenced. So this object should disapear from memory. But this doesn't happens since plt.show() draws the curve on a graphic window. So how does it work ? Hi, I have not checked the source code, but pyplot probably implicitly generates a few objects for you. In particular it probably creates a default figure, so when you say plt.plot(x,y), behind the scenes pyplot will request the current figure and add the Line2D items to it. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: operator module functions
For me it makes sense. operator.add should be used in a global context (I don't know how to express it otherwise). So you provide it with the two values that you want to add. The .__add__ variants are bound to a particular instance and you provide it with a single value that you want to add. You also need the dunder versions when you want to implement addition for user defined types. So although they are similar, I do believe they have slightly different uses. As an example, you cannot use the dunder versions for literals. 2.__add__(3) # Oops, does not work a = 2 a.__add__(3) 5 import operator operator.add(2,3) # Fine I also think operator.add versus .__add__ is equivalent to the global getattr() and .__getattr__. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Recommended hosting
On Thursday, October 2, 2014 3:30:38 PM UTC+2, writeson wrote: Hi all, I'd like to build a web site for myself, essentially a vanity web site to show off whatever web development skills I have, and perhaps do some blogging. I'm a Python developer, so I'd like to develop the site with the following stack: web applications written with Python and Flask, running as uwsgi applications. These would support dynamic HTML where needed, but mostly it would provide REST API's. static content delivered by Nginx Can anyone give me some recommendations for a good hosting company that would allow me work with the above tool set? I'm US based if that makes a difference. Thanks in advance! Doug Doug, Also don't forget the new kid on the block Docker! So, you can build your webapp locally on your machine, deploy it (also locally) in a Docker container. Test it and then deploy it in the cloud. You have lots of options for this. I am the process of doing this myself and I like it a lot! Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: dict to boolean expression, how to?
Hi, Are you aware of the Python operator module? It provides function equivalents of all (most?) python operator. So instead of a==b, you can state operator.eq(a,b). As a result, you can loop over the key/value pairs in the dict and built your logic with the operator.eq, operator.and_, and operator.or_ (notice the trailing underscore to avoid clashing with normal and and or statements. Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Standard library Help
On Friday, July 11, 2014 10:32:32 AM UTC+2, Nicholas Cannon wrote: Hey i would like to know alot more about the standard library and all of its functions and so on and i know it is huge and i would basically like to learn only the useful stuff that i could use and all of those features. i have been looking around and i cant really find anything so i wondering if you guys would know any places to learn it. Hi Nicholas, Have you tried the library reference [1]? If so, can you explain why it is not sufficient for your needs? [1] https://docs.python.org/3/library/index.html -- https://mail.python.org/mailman/listinfo/python-list
Re: how can i get body values?
On Friday, July 11, 2014 11:37:17 AM UTC+2, Frank Liou wrote: how can i get body values use variable to Separate catch? https://lh3.googleusercontent.com/-6Ywp4GukuCM/U7-vhF0nzuI/Bv4/Ovcr1O2FScs/s1600/321.jpg i want to catch name and key values now i use request.data i can catch all the body but how can i use variable to separate and catch? Hi Frank, In general, you will get better response from the group if you post the code that you have so far and ask questions on why it doesn't work or how to proceed. In case of your particular problem, the answer is it depends. Do you use the Python standard library, or a webserver framework like tornado or cherrypy? If you still have a choice, I would recommend to use a framework like cherrypy. It makes your life a lot easier. Regards, Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Python While loop Takes too much time.
On Monday, June 30, 2014 1:32:23 PM UTC+2, Jaydeep Patil wrote: I have did excel automation using python. In my code I am creating python dictionaries for different three columns data at a time.There are are many rows above 4000. Lets have look in below function. Why it is taking too much time? Code: def transientTestDict(self,ws,startrow,startcol): self.hwaDict = OrderedDict() self.yawRateDict = OrderedDict() rng = ws.Cells(startrow,startcol) while not rng.Value is None: r = rng.Row c = rng.Column time = rng.Value rng1 = rng.GetOffset(0,1) hwa = rng1.Value rng2 = rng.GetOffset(0,2) yawrate = rng2.Value self.hwaDict[time] = hwa,rng.Row,rng.Column self.yawRateDict[time] = yawrate,rng.Row,rng.Column rng = ws.Cells(r+1,c) Please have look in above code suggest me to improve speed of my code. Regards Jaydeep Patil Hi Jaydeep, I agree with Peter. I would avoid moving from cell to cell through the EXCEL interface if you can avoid. If possible, I would try to read ranges from EXCEL into a python list (or maybe numpy arrays) and do the processing in Python. In the past I even dumped an EXCEL sheet as a CSV file and then used the numpy recfromcsv function to process the data. If you are really brave, dump EXCEL alltogether :) and do all the work in Python (have you already tried IPython notebook?). Regards, Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Why isn't this code working how I want it to?
On Saturday, October 12, 2013 10:56:27 AM UTC+2, reuben...@gmail.com wrote: I've been working on a program and have had to halt it due a slight problem. Here's a basic version of the code: a = 'filled' b = 'filled' c = 'empty' d = 'empty' e = 'filled' f = 'empty' g = 'filled' testdict = {a : 'apple' , b : 'banana' , c : 'cake' , d : 'damson' , e : 'eggs' , f : 'fish' , g : 'glue'} Now what I want to do, is if a variable is filled, print it out. This however isn't working how I planned. The following doesn't work. for fillempt in testdict: if fillempt == 'filled': print(testdict[fillempt]) All this does though, is print glue, where I'd want it to print: apple banana eggs glue Perhaps a dictionary isn't the best way to do this.. I wonder what else I can do... Thanks for any help. Hi, Remember that keys in a dictionary are unique. So if you defined ( means it I typed it at the interactive terminal prompt, d = { 'filled' : 'apple' , 'filled' : 'orange' } and do a print d it will show: {'filled': 'orange'} One way to solve this problem is to define two dictionaries. One holding the status of the variable, the other one holding the data. For example: status = { 'a' : 'filled', 'b' : 'empty', 'c' : 'filled' } data = { 'a' : 'orange', 'b' : 'apple', 'c' : 'banana' } for k in status: if status[k]=='filled': print data[k] Regards and let us know if it works for you, Marco -- https://mail.python.org/mailman/listinfo/python-list
Re: Printing a text over an image
On Wednesday, November 7, 2012 5:52:36 PM UTC+1, Martha Morrigan wrote: Hi guys, Using python, wxpython and sqlite in a windows system, Im trying to print some certificates/diplomas/cards with a image at background with the name of person/text over it. I know the basic steps to print the text using win32print from Pywin32 but...: 1) I dont know how to add an image and set it to background. while . . # Query sqlite rows and collumn name and set the self.text for each certificate . # Now send to printer DC = win32ui.CreateDC() DC.CreatePrinterDC(win32print.GetDefaultPrinter()) DC.SetMapMode(win32con.MM_TWIPS) DC.StartDoc(Certificates Job) DC.StartPage() ux = 1000 uy = -1000 lx = 5500 ly = -55000 DC.DrawText(self.text, (ux, uy, lx, ly),win32con.DT_LEFT) DC.EndPage() DC.EndDoc() This printer-code is inside a while loop calling each people name from a sqlite database per check condition. 2) All the names of database was printed at same page... how i command the printer to spit out 1 page per name from the database? 3) Any more simple approach or module to deals with printers (paper and/or pdf) will be welcome. Thanks in advance, Martha Hi Martha, Since you are on windows, why don't you use MS/Word directly from Python. It has all the abstractions you need (documents, pages, tables, figures, printing etc.). Working with MS/Word through the win32 bindings is really simple. I have done this a while ago for some automatic report generation. The basic routine is to start recording your actions with the MS/Word macro recorder, do the things you want to do, stop recording, look at the VB code and guess the equivalent Python code. This is not as bad as it sounds. It normally is really straightforward. I am on Linux at the moment, so I cannot present any code examples. Feel free to try and post some example code if you get stuck. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: What's the tidy/elegant way to protect this against null/empty parameters?
On Monday, October 15, 2012 1:33:02 PM UTC+2, (unknown) wrote: I want to fix an error in some code I have installed, however I don't really want to just bodge it. The function producing the error is:- def get_text(self, idx): # override ! node = self.items[idx] a= [ , .join(node.tags), node.comment, node.folderName, cd2rd(node.date), node.name, '[' + self.rating_stars[node.rating] + ']' ] [self.select] return a The error occurs when node[] (or at least its members) turn out to be empty, you get a Traceback that ends with:- File /usr/lib/jbrout/jbrout/listview.py, line 608, in draw_cell layout.set_text(self.get_text(thumbnail_num)) File /usr/lib/jbrout/jbrout.py, line 325, in get_text , .join(node.tags), TypeError: sequence item 0: expected string, NoneType found Now its *probably* something higher up the tree causing the problem (it's only one particular image in 20 thousand or so that breaks things) but I really want to just get things working. So, what's the neatest way to protect the get_text() method from empty data? -- Chris Green Hi, Instead of protecting against empty data, you could just catch the exception, issue a warning and return a default error node which is valid. So something like (not tested): def get_text(self, idx): # override ! node = self.items[idx] error_a = A valid, but erroneous representation of a try: a= [ , .join(node.tags), node.comment, node.folderName, cd2rd(node.date), node.name, '[' + self.rating_stars[node.rating] + ']' ] [self.select] except TypeError: print 'Oops, something went wrong' a = error_a # You should always have a valid a here (or another exception has occured) return a -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessing variables in __init__.py
On Tuesday, October 16, 2012 10:48:17 AM UTC+2, Gaudha wrote: my_package/ __init__.py my_module1.py my_module2.py variables.py I want to define common variables in __init__.py and use the namespace in my_module1.py or my_module2.py. Defining it is not a problem. How can call it from my modules? If I define them in a module (say, variables.py), I can call them by importing variables.py in other modules. How can it be done if I define it in __init__.py? It may be a silly query as I am newbie in Python. But, I would be grateful to get help. Hi, If you store the variables in __init__.py, you can import them from the package. So in your case suppose __init__.py contains: a = 10 b = {1 :Hello, 2: World } Than if you import my_package, you can access the variables as follows (interactive IPython session): In [1]: import my_package In [2]: my_pack my_package my_package/ In [2]: my_package. my_package.a my_package.b In [2]: my_package.a Out[2]: 10 In [3]: my_package.b Out[3]: {1: 'Hello', 2: 'World'} In [4]: -- http://mail.python.org/mailman/listinfo/python-list
Re: Providing a Python wrapper to a C++ type.
On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote: Hi, I have a C++ module where I have a defined, working type. How would I make a wrapper for this type to be able to be used in Python? I am familiar(-ish) with the C-API for functions but I can't see concretely how one would include an interface to a type. Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html Regards, Aaron Hi Aaron, There are a few ways of doing this. At least three come to my mind: 1. Wrap the C++ type yourself by using handcrafted code implemented with the Python C API 2. Use SWIG to wrap the C++ code and (semi) automatically create the wrapper (http://www.swig.org/) 3. Use BOOST Python to wrap the C++ code (http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html) I would highly discourage (1) unless you are very brave and curious. Ofcourse it is a nice excercise, but if you want something to work quickly I would recommend to use either (2) or (3). I have used both SWIG and BOOST Python and either of them worked pretty well for me. In the end I selected BOOST Python, because I was only interested in the Python wrapping (SWIG could generate many other wrappers as well). Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to create kernel log messages via Python?
On Tuesday, October 16, 2012 5:43:28 AM UTC+2, J wrote: Hi... I have a bit of code that does the following: uses the syslog module to inject a LOG_INFO message into the syslog on my linux machine runs a suspend/resume cycle uses the syslog module to inkect a LOG_INFO message marking the end of test. Then I parse everything between the start and stop markers for certain items that the Linux kernel logs during a suspend and resume cycle. But my resume complete timing is not as accurate as it could be. The problem with doing it this way is that while I can find definite kernel messages that mark various points in the suspend/resume cycle, the final message when the kernel is done resuming is NOT the point I actually want to mark. Instead, the end point I want is the time of the ending marker itself, as this happens after certain other things are done such as resumption of networking services. Here's the problem. I can't just use syslog timestamps. The reason is that the syslog timestamps are only indicative of when messages are written to syslog via syslogd. The kernel timestamps are different. For example, the following bits of log are taken from the time the test starts until the end of the going to sleep kernel messages. First, note that there's a 5 second difference between the START marker and the first kernel message. Next, look at the kernel timestamps. The total real time to suspend starts at 421320.380947 and ends at 421322.386355, around 2 seconds later, where the log messages themselves all state that the events occurred at the same time. Oct 15 10:24:19 klaatu sleep_test: ---SLEEP TEST START 1350296656--- Oct 15 10:25:24 klaatu kernel: [421320.380947] PM: Syncing filesystems ... done. Oct 15 10:25:24 klaatu kernel: [421320.391282] PM: Preparing system for mem sleep [SNIP] Oct 15 10:25:24 klaatu kernel: [421322.282943] Broke affinity for irq 23 Oct 15 10:25:24 klaatu kernel: [421322.386355] CPU 7 is now offline So, what I REALLY want is to inject my start/stop markers into klogd rather than syslogd. This will, I hope, give my markers kernel timestamps rather than syslog timestamps which are not as accurate. So does anyone know of a way to do this? Unfortunately, I've tried some searching but google doesn't like the term klog, and most of the hits involved injecting code or other things that are not related at all. Or, if there's a better way to get accurate timestamps, what would that be? Hi, I cannot be of too much help here, but is there a userspace/kernel C-interface that could be used. If so, you might be able to use the Python ctypes module to create messages at the correct moments Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Which book is the best?
On Tuesday, October 16, 2012 6:41:29 AM UTC+2, David Hutto wrote: On Tue, Oct 16, 2012 at 12:27 AM, 老爷 yujian4newsgr...@gmail.com wrote: I have strong c++ development experience. But now I want to study the python to do some windows setting task, such as editing file, changing the system setting, doing some network processing. Please help me which book is the best? Definitely command line apps/command line usage. I could recommend google searches, but use the calls to the OS, and you can accomplish a good bit of things. -- Best Regards, David Hutto CEO: http://www.hitwebdevelopment.com Hi, Although I agree with the fact the working in the interactive interpreter (may I recommend IPython for this) is definitely an efficient way of exploring the Python world, I also liked alot the Python Essential Reference (4th edition). Since you already understand how to program, the Python essential reference quickly guides you through Python language and the standard library. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: numpy - 2D matrix/array - initialization like in Matlab...
On Tuesday, October 16, 2012 12:43:09 AM UTC+2, someone wrote: On 10/15/2012 11:26 PM, MRAB wrote: On 2012-10-15 22:09, someone wrote: See this: == In [5]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 1.5') In [6]: Dx Out[6]: matrix([[ 1. , 0. , 0. ], [ 0. , 0.5, -0.5], [ 0. , -0.5, 1.5]]) == Ok... So now test = 33 and instead of the value 1.5 I want to use the value of test and put it directly into the matrix (or array): == In [7]: test=33 In [8]: Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test') --- NameError Traceback (most recent call last) /home/user/something/ipython-input-8-5a43575649e1 in module() 1 Dx = numpy.matrix('1 0 0; 0 0.5 -0.5; 0 -0.5 test') /usr/lib/python2.7/dist-packages/numpy/matrixlib/defmatrix.pyc in __new__(subtype, data, dtype, copy) 252 253 if isinstance(data, str): -- 254 data = _convert_from_string(data) 255 256 # now convert data to an array .. etc... == So obviously it doesn't understand that I want this: == In [21]: Dx[2,2]=test In [22]: Dx Out[22]: matrix([[ 1. , 0. , 0. ], [ 0. , 33. , -0.5], [ 0. , -0.5, 33. ]]) == Without having to manually change all the individual places using my variables (test is actually many variables, not just one but I think you should understand the problem now). How to initialize my array directly using variables ? It could also be that I wanted: test11 = 1 test12 = 1.5 test13 = 2 test21 = 0 test22 = 5 Dx = numpy.matrix('test11 test12 test13; test21 test22 -0.5; 0 -0.5 1.5') Etc... for many variables... Appreciate ANY help, thank you very much! What it prints should give you a hint: Dx = numpy.matrix([[test11, test12, test13], [test21, test22, -0.5], [0, -0.5, 1.5]]) Dx matrix([[ 1. , 1.5, 2. ], [ 0. , 5. , -0.5], [ 0. , -0.5, 1.5]]) Uh, great - thank you very much! As you maybe see, I'm only a python newbie so I'm not so good at understanding the error messages and reading the source code yet. Thank you very much for the solution to the problem! It's highly appreciated. Thanks. Hi, Also note that you don't need to initialize the array with a string. You could directly do it like this: a = numpy.array(((1,2,3), (2,3,4), (4,5,6))) Other things that might be interesting for you are: # List comprehension (standard python) to convert strings to floats vals = [ float(s) for s in 1.0 2.3 1.2.split() ] produces [1.0, 2.3, 1.2] vals = [ float(s) for s in (1.0, 2.3, 1.2) ] produces again [1.0, 2.3, 1.2] Also lookup the documentation for numpy.reshape. With this you could provide a single list of for example 9 numbers and reshape it into a 3x3 array. Python and Numpy are so cool!! Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Python on Windows
On Tuesday, October 16, 2012 1:29:23 PM UTC+2, graham wrote: Downloaded and installed Python 2.7.3 for windows (an XP machine). Entered the Python interactive interpreter/command line and typed the following: import feedparser and I get the error message No module named feedparser. There is a feedparser.py file lurking around - so I suppose Python cannot find it. Anyone: What to do? GC Hi, feedparser.py is not a Python standard library. So, if it feedparser is located in a non-standard folder you have at least the following two options: 1. Update the PYTHONPATH environment variable such that it includes the path the installation location of feedparser.py 2. Add the path to feedparser.py directly in the script that uses it. Something like the following: import sys sys.path.append(path to feedparser.py) import feedparser Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Providing a Python wrapper to a C++ type.
On Tuesday, October 16, 2012 1:39:44 PM UTC+2, Stefan Behnel wrote: Marco Nawijn, 16.10.2012 12:17: On Tuesday, October 16, 2012 10:11:52 AM UTC+2, aaron.l...@gmail.com wrote: I have a C++ module where I have a defined, working type. How would I make a wrapper for this type to be able to be used in Python? I am familiar(-ish) with the C-API for functions but I can't see concretely how one would include an interface to a type. Is it this? http://docs.python.org/release/2.7.3/extending/newtypes.html There are a few ways of doing this. At least three come to my mind: 1. Wrap the C++ type yourself by using handcrafted code implemented with the Python C API 2. Use SWIG to wrap the C++ code and (semi) automatically create the wrapper (http://www.swig.org/) 3. Use BOOST Python to wrap the C++ code (http://www.boost.org/doc/libs/1_51_0/libs/python/doc/index.html) I would highly discourage (1) unless you are very brave and curious. Ofcourse it is a nice excercise, but if you want something to work quickly I would recommend to use either (2) or (3). I have used both SWIG and BOOST Python and either of them worked pretty well for me. In the end I selected BOOST Python, because I was only interested in the Python wrapping (SWIG could generate many other wrappers as well). There's also Cython, which provides a very flexible way (unlike SWIG) of doing these things easily (unlike C++ with Boost). http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html I agree with discouraging 1) in specific. Stefan Hi Stefan, I never worked with Cython (but I know it is very powerful and interesting) but in my mind there are slight differences in usage scenario between e.g. Boost Python and Cython. For me the idea of Cython is that your main code is in Python, but you want to improve the performance of specific parts of the code. In that case, Cython is the way to go. In case of Boost Python, the scenario for me is that you have a main program/library in C++, but you want to be able use the functionality from Python. Do you agree with this view? Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: python CAD libraries?
On Monday, September 10, 2012 11:10:55 PM UTC+2, Jayden wrote: Are there any python CAD libraries that can (1) build simple 3D primitives solids such as spheres, cylinders and so on (2) perform bool operations on 3D solids (3) better if it has some transformations such has scaling, sweeping, and lofting Please recommend some good ones for me? Thanks a lot!! Hi Jayden, In my opinion, the best you can get is OpenCascade (OCC) (www.opencascade.org) in combination with the python bindings (www.pythonocc.org). OCC is a hugh C++ CAD library. It not only deals with the simple geometric stuff, but it can be used to build CAD programs similar to SolidEdge or SolidWorks. It does however come with quite a steep learning curve. When using PythonOCC, the learning curve becomes a little less steep. Also note that in my opinion, Blender cannot be considered as a CAD environment. Ofcourse it is very powerful, but I think it is more targeted towards animation and visually pleasing applications, not mechanical engineering. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: pythonOCC examples doesn't work?
On Wednesday, September 12, 2012 6:02:14 AM UTC+2, Jayden wrote: I installed (1) pythonxy2.7.2.3 (with python2.7) and (2) pythonOCC-0.5-all-in-one.win32.py26 on windows 7 64 bit computer. I try run pythonOCC examples in its example folder, such as the helloworld.py and got errors as follows: ImportantError: DLL load failed: The specified module could not be found. The error come from the line of code: from OCC.BrepPrimAPI import * How to fix the error? Thanks a lot!! Hi Jayden, It has been some time ago that I used PythonOCC and I used it on Linux, so I cannot be of much help here. It sounds like you have to tell Windows where to look for the installed libraries (environment variables?). Anyhow, I recommend to post the question to the PythonOCC mailinglist. They are quite responsive. One last suggestion. OCC itself comes with a small utility called DRAWEXE. It is a tcl/tk program that can be used to play around with a lot of the functionality provided by OCC. Good luck! Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for an IPC solution
On Friday, August 31, 2012 9:22:00 PM UTC+2, Laszlo Nagy wrote: There are just so many IPC modules out there. I'm looking for a solution for developing a new a multi-tier application. The core application will be running on a single computer, so the IPC should be using shared memory (or mmap) and have very short response times. But there will be a tier that will hold application state for clients, and there will be lots of clients. So that tier needs to go to different computers. E.g. the same IPC should also be accessed over TCP/IP. Most messages will be simple data structures, nothing complicated. The ability to run on PyPy would, and also to run on both Windows and Linux would be a plus. I have seen a stand alone cross platform IPC server before that could serve channels, and send/receive messages using these channels. But I don't remember its name and now I cannot find it. Can somebody please help? Thanks, Laszlo Hi, Are you aware and have you considered zeromq (www.zeromq.org)? It does not provide a messaging system, but you could use things like simple strings (json) or more complicated things like Protobuf. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Beginners question
On Thursday, August 30, 2012 1:54:08 PM UTC+2, (unknown) wrote: Hello I'm slowly teaching myself python so apologies if this is a dumb question. but something has confused me with the os.stat() function: s = os.stat(.) print s posix.stat_result(st_mode=16877, st_ino=2278764L, st_dev=2053L, st_nlink=2, st_u id=1000, st_gid=100, st_size=4096L, st_atime=1346327745, st_mtime=1346327754, st _ctime=1346327754) What sort of object is posix.stat_result? Its not a dictionary or list or a class object as far as I can tell. Thanks for any help. B2003 Hi, So let's try to figure this out. First of all, we can ask Python what object it is. s = os.stat('.') type(s) posix.stat_result So it seems to be a custom type. However types can inherit from builtins like list, tuple and dict, so maybe it still is a dict or a tuple. Let's ask Python again: isinstance(s, dict) False isinstance(s, (tuple, list)) False Ok. So it is neither a list (tuple) nor a dict. So without reverting to the source code, it is probably save to say that the result is a custom class where the attributes can be accessed by the dot '.' notation. This is confirmed when you do: dir(s) .. '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'n_fields', 'n_sequence_fields', 'n_unnamed_fields', 'st_atime', 'st_blksize', 'st_blocks', 'st_ctime', 'st_dev', 'st_gid', 'st_ino', 'st_mode', 'st_mtime', 'st_nlink', 'st_rdev', 'st_size', 'st_uid'] For example: print s.st_size 4096 In case of Linux I think that the result of os.stat(..) is a wrapping of a C struct (a class with only attributes and no methods). A small additional remark. Besides being a real dict or list (by means of inheritance), custom class can also implement the interface (__getitem__ etc.). If you want to know if an object implements this interface you could use the types defined in the 'abc' and 'collections' standard modules. So instead of checking if a type is a dict like this: isinstance(s, dict) you could also check if it implements the dict interface: isinstance(s, collections.MutableMapping) # or similar Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: class object's attribute is also the instance's attribute?
On Thursday, August 30, 2012 12:55:25 PM UTC+2, 陈伟 wrote: when i write code like this: class A(object): d = 'it is a doc.' t = A() print t.__class__.d print t.d the output is same. so it means class object's attribute is also the instance's attribute. is it right? i can not understand it. I think the best way is to think of it as a global attribute restricted to the class A. Note that you even don't need an instance to get the value of the attribute. You can directly get it from the class itself. class A(object): d = 'my attribute' A.d 'my attribute' aobj = A() aobj.d 'my attribute' Note that if you change 'd' it will change for all instances! bobj = A() bobj.d 'my attribute' A.d = 'oops...attribute changed' aobj.d 'oops...attribute changed' bobj.d 'oops...attribute changed' If you want attributes to be local to the instance, you have to define them in the __init__ section of the class like this: class A(object): def __init__(self): d = 'my attribute' aobj = A() bobj = A() aobj.d 'my attribute' bobj.d 'my attribute' aobj.d = 'oops...attribute changed' aobj.d 'oops...attribute changed' bobj.d 'my attribute' Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Beginners question
On Thursday, August 30, 2012 3:15:03 PM UTC+2, Ulrich Eckhardt wrote: Am 30.08.2012 13:54, schrieb boltar2003@boltar.world: s = os.stat(.) print s posix.stat_result(st_mode=16877, st_ino=2278764L, st_dev=2053L, st_nlink=2, st_u id=1000, st_gid=100, st_size=4096L, st_atime=1346327745, st_mtime=1346327754, st _ctime=1346327754) What sort of object is posix.stat_result? Use the type() function to find out. I guess that this is a named tuple, which is a tuple where the attributes are not indexed but have a name, see the documentation for the namedtuple() function from the collections library. Uli It is not a namedtuple. Because a namedtuple is a tuple and therefore isinstance(s, tuple) would have returned True. from collections import namedtuple Point = namedtuple('Point', 'x y') p = Point(10,2) isinstance(p, tuple) True -- http://mail.python.org/mailman/listinfo/python-list
Re: class object's attribute is also the instance's attribute?
On Thursday, August 30, 2012 3:25:52 PM UTC+2, Hans Mulder wrote: On 30/08/12 14:34:51, Marco Nawijn wrote: Note that if you change 'd' it will change for all instances! That depends on how you change it. bobj = A() bobj.d 'my attribute' A.d = 'oops...attribute changed' Here you change the attribute on the class. That will affect all instances: aobj.d 'oops...attribute changed' bobj.d 'oops...attribute changed' You can also set the attribute on an instance: bobj.d = 'For bobj only' bobj.d 'For bobj only' aobj.d 'oops...attribute changed' So, if you specifically change it on one instance, thenit won't change on other instances of the same class. If you want attributes to be local to the instance, you have to define them in the __init__ section of the class like this: That's a good idea, but it's not required. You can set them later, as shown above. class A(object): def __init__(self): d = 'my attribute' That will just set the global variable d. You want to set the instance attribute: self.d = 'my attribute' aobj = A() bobj = A() aobj.d 'my attribute' Note that aobj.d will not find the global variable d, if neither the instance, nor the class nor any of the base classes have that attribute. I don't know where this 'my attribute' comes from, but it's not the instance attribute you tried to set in the __init__ method. Maybe your class A still has a class attribute with that value from an earlier experiment. Hope this helps, -- HansM Learned my lesson today. Don't assume you know something. Test it first ;). I have done quite some programming in Python, but did not know that class attributes are still local to the instances. It is also a little surprising I must say. I always considered them like static variables in C++ (not that I am an expert in C++). I knew of course that you don't have to define a local attribute in the __init__ method of a class, but I consider it good style and since the OP is a self claimed newbie I left out the other option. The missing self in the code below was a typo class A(object): def __init__(self): d = 'my attribute' # should be self.d Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: class object's attribute is also the instance's attribute?
On Thursday, August 30, 2012 4:30:59 PM UTC+2, Dave Angel wrote: On 08/30/2012 10:11 AM, Marco Nawijn wrote: On Thursday, August 30, 2012 3:25:52 PM UTC+2, Hans Mulder wrote: snip Learned my lesson today. Don't assume you know something. Test it first ;). I have done quite some programming in Python, but did not know that class attributes are still local to the instances. They're not. They're just visible to the instances, except where the instance has an instance attribute of the same name. Don't be confused by dir(), which shows both instance and class attributes. Please show me an example where you think you observe each instance getting a copy of the class attribute. There's probably some other explanation. I don't have an example. It was just what I thought would happen. Consider the following. In a class declaration like this: class A(object): attr_1 = 10 def __init__(self): self.attr_2 = 20 If I instantiated it twice: obj_1 = A() obj_2 = A() For both obj_1 and obj_2 attr_1 equals 10. What I thought would happen after the following statement: obj_1.attr_1 = 12 is that obj_2.attr_1 also equals 12. This is what surprised me a little, that's all. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamically creating classes from text
On Jan 24, 6:22 pm, T H turian9...@gmail.com wrote: I’m new to python, sorry if my question is a bit naive, I was wondering if it is possible to parse some text (ie. from a text file or say html) and then dynamically create a class? for example lets say the contents of the text file is: functionName: bark arg1: numberBarks functionName: run arg1: howFast arg2: howLong and then from the text dynamically create a class like the one below (including the functions and its implementation) class Dog: def bark(self, numberBarks, myArg): print(‘numberBarks: ‘ + numberBarks) print(‘myArg’ + myArg) return def run(self, howFast, howLong, myArg): print(‘howFast: ‘ + howFast) print(‘howLong’ + howLong) print(‘myArg’ + myArg) return I know my question is a bit far fetched. Anyhow if it is possible how is it done? Thanks so much for any help! Hi, You could also take a look at the 'type' builtin. It can be used to dynamically create a class. It allows you to specify the class name, the bases and a dictionary containing the attributes. A simple example: aClass = type('A', (object, ), { 'a' : 10, 'b' : 'Hello World' }) aobj = aClass() aobj.__class__.__name__ 'A' aobj.a 10 The methods might be a little more difficult to attach to the class, but I don't fully understand your problem. In particular, where does the implementation of your methods come from? Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiprocessing bug, is my editor (SciTE) impeding my progress?
On Dec 6, 8:13 pm, John Ladasky lada...@my-deja.com wrote: Hi, folks, Back in 2002, I got back into programming after a nine-year hiatus. I needed a new programming language, was guided to Python 2.2, and was off to the races. I chose the SciTE program editor, and I have been using it ever since. I'm now using Python 2.6 on Ubuntu Linux 10.10. My programming needs have grown more sophisticated, but I'm still using SciTE. Pretty much all of my recent posts to comp.lang.python have concerned multiprocessing. I put together a decent system for my current project, and had it all working. Then I realized that I needed to refactor and expand some code, which I did -- and somehow, I generated a bug that I simply cannot understand. I've been puzzling over it for three days. The error is occurring inside one of my subprocesses. As far as I know, SciTE is limited in what it can do in this situation. The program does not return when a subprocess generates an exception. I see the error message, but then the program simply hangs. I have tried invoking the subprocess directly without scheduling it through multiprocessing.Pool. It works fine. So the problem is occurring inside Pool. I tried opening my code in IDLE, and figured I could step through it, or at least call functions one line at a time. It appears that multiprocessing code is not compatible with IDLE. IDLE simply crashes when I try to invoke any of the important functions. I know, you want me to post a minimal example. Most of the time, that's possible, and I do it. Trust me, this time it isn't. I have about 500 lines of code, split across three files. These implement a neural network, some test data, and multiprocessing methods for network evaluation. I made several concerted changes to the code, and turned a working system into this: = Exception in thread Thread-1: Traceback (most recent call last): File /usr/lib/python2.6/threading.py, line 532, in __bootstrap_inner self.run() File /usr/lib/python2.6/threading.py, line 484, in run self.__target(*self.__args, **self.__kwargs) File /usr/lib/python2.6/multiprocessing/pool.py, line 225, in _handle_tasks put(task) TypeError: expected string or Unicode object, NoneType found = Here's what I think would help me debug this error: I would like to catch the TypeError, and examine the contents of task. I need to accomplish this WITHOUT adding a try...except block to the Python library file multiprocessing/pool.py. I don't know whether this is possible, because the traceback isn't clear about where my OWN code calls the code which is generating the error. After that, if the cause of the error still is not obvious, I might need to go back to the working program. Somehow I want to examine the contents of task when the program works, and no TypeError is being generated. By comparing the two, I hope to see a difference. From that, I should be able to figure out how I have broken what is being fed to Pool.__init__ and/or MapResult.__init__. Any suggestions how I might best accomplish this task? Does this error message look familiar to anyone? More generally, should I consider graduating from SciTE? I have had a look at a few of the more comprehensive IDE's over the years, and I'll have to say that I found them to be intimidating. I found it to be a huge chore just to open a single Python script and run it inside an IDE. It seems like you had to know how to set up a complete, multi- script project before you could even accomplish simple tasks. That steep learning curve is the reason that I didn't choose Java as my programming language. So, if any of you have pertinent recommendations in the IDE department, please feel free to guide me that way. Thanks! Hello John, One way of trying to debug the issue could be to use ipython and ipdb. You cadn than run your code from within the ipython shell. The debugger will hold at the type error, but keep the context. At this point you should be able to evaluate task. As a side comment to your IDE remarks. I keep switching between VIM and Aptana/Pydev. The more I learn about VIM the more I feel comfortable and productive. In combination with ipython it is quite a solid development environment. On the other hand Pydev is very user friendly, powerfull and easy to learn. Debugging in Pydev is excellent. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Execute a command on remote machine in python
On Nov 15, 1:04 pm, Roark suha...@gmail.com wrote: Hi, I am first time trying my hands on python scripting and would need some guidance from the experts on my problem. I want to execute a windows command within python script from a client machine on a remote target server, and would want the output of the command written in a file on client machine. What is the way it could be achieved. Thanks in advance, Roark. Hello Roark, If the command does not change over time, an option could be to encapsulate the command and the output behind an XML-RPC interface. I used it several times now and for me this works perfectly. XML-RPC is part of the Python standard library, so it should work out of the box on windows and linux. It also supports mixed programming languages (maybe C# on windows to get the info you want and python on linux on the client). Kind regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Python ORMs Supporting POPOs and Substituting Layers in Django
On Nov 5, 9:11 pm, Travis Parks jehugalea...@gmail.com wrote: Hello: A new guy showed up at work a few weeks ago and has started talking about replacing a 6 month old project, written in ASP.NET MVC, with an open source solution that can handle massive scaling. I think his primary concern is the potential need for massive web farms in the future. In order to prevent high licensing costs, I think he wants to move everything to open source technologies, such as the LAMP stack. I also don't think he truly understands what ASP.NET MVC is and thinks it is the older WebForms. I have been researching open source MVC frameworks and came across Django. It looks like an awesome tool, but I am willing to look at others. I have experience in Python (and enough in PHP to want to avoid it and absolutely none in Ruby) so I think it would be a good language to develop in. I was wondering if there were any ORMs for Python that used POPOs (plain old Python objects). There is a lot of business logic in my system, and so I want to keep my data objects simple and stupid. I want the ORM to be responsible for detecting changes to objects after I send them back to the data layer (rather than during business layer execution). Additionally, being a stateless environment, tracking objects' states isn't very useful anyway. Honestly, I doubt this guy is going to get his wish. The people paying for the application aren't going to be willing to throw 6 months of work down the drain. Never the less, I want to have plenty of research under my belt before being asked what my thoughts are. He was talking about using the Zend Framework with PHP, but I want to avoid that if possible. Django seems like one of the best MVC solutions in the Python arena. I would be willing to replace Django's ORM solution with something else, especially if it supported POPOs. I could even map all of the non-POPOs to POPOs if I needed to, I guess. Finally, I wanted to ask whether anyone has tried having Django call out to Python 3 routines. I am okay using Python 2.7 in Django, if I can have the controllers call business logic implemented in Python 3, accepting POPOs from the data layer. Django would really just be a coordinator: grab data from Django ORM, convert results into POPOs, load up Python 3 module with business logic, passing POPOs, returning POPOs and then converting those to view models. I'm sweating just thinking about it. My guess is that there would be a severe penalty for crossing process boundaries... but any insights would be appreciated. Thanks, Travis Parks Hello Travis, I am not an expert in the field, but I have used SQLAlchemy (www.sqlalchemy.org) for a while and was very happy with it. It should be able to scale up to pretty complex applications and large amounts of data. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Tree structure
On Jul 26, 6:53 am, Bevan Jenkins beva...@gmail.com wrote: Hello, I am trying to create a tree structure for use with a PyQt QTreeView. But first I need to get my head around how to create the tree structure. I have a dictionary (for testing purposes) but I will later use a table via sqlalchemy. The use case is hydrology, so I would like to have a hydrologically connected river tree, in which you can browse upstream from the sea (making choices) or downstream from any named hydrological feature. Each key flows into its value pair. myrivers = {river:flows_into}. An example is below: myrivers = {little stream:sea, mountain stream:lake, lake:big river, cold spring:big river, big river:sea sea:} I would like the tree to look like (if the formatting works). so you can browse downstream from each named river but also upstream from the sea picking which direction to go. little stream sea mountain stream lake big river sea lake big river sea cold spring big river sea big river sea sea little stream big river lake mountain stream cold spring this next part is probably not very clear So every key is a parent. For all keys that have a value (not ), the value is the child and is then used as a parent to get the next child until the sea and a value of is reached. For the sea this is reversed, that you find all rivers that flow into the sea and then all rivers that flow into them. Any thoughts about how to acomplish this will be much appreciated, Bevan Hello Bevan, Is it an option to use XML as an in-memory representation. It naturally provides the interface you need, like traversing from parent to children and back. In addition, you get querying capabilities like XPATH for free. In python I recommend lxml. Your task than is to fill the tree based on the information in the database. The ORM functionality from sqlalchemy will be of help here. In addition, you somehow have to populate the QT tree with the data from your in-memory XML representation. I have no experience with QT so I cannot help you there. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: losing-end-of-row values when manipulating CSV input
On Jul 13, 10:22 pm, Neil Berg nb...@atmos.ucla.edu wrote: Hello all, I am having an issue with my attempts to accurately filter some data from a CSV file I am importing. I have attached both a sample of the CSV data and my script. The attached CSV file contains two rows and 27 columns of data. The first column is the station ID BLS, the second column is the sensor number 4, the third column is the date, and the remaining 24 columns are hourly temperature readings. In my attached script, I read in row[3:] to extract just the temperatures, do a sanity check to make sure there are 24 values, remove any missing or m values, and then append the non-missing values into the hour_list. Strangely the the first seven rows appear to be empty after reading into the CSV file, so that's what I had to incorporate the if len(temps) == 24 statement. But the real issue is that for days with no missing values, for example the second row of data, the length of the hour_list should be 24. My script, however, is returning 23. I think this is because the end-of-row-values have a trailing \. This must mark these numbers as non-digits and are lost in my isdig filter line. I've tried several ways to remove this trailing \, but to no success. Do you have any suggestions on how to fix this issue? Many thanks in advance, Neil Berg csv_test.py 1KViewDownload csv_sample.csv 1KViewDownload Dear Neil, Don't know if this is a double post (previous post seems to be gone), but val = val.rstrip('\\') should fix your problem. Note the double backslash. Kind regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: losing-end-of-row values when manipulating CSV input
On Jul 13, 10:22 pm, Neil Berg nb...@atmos.ucla.edu wrote: Hello all, I am having an issue with my attempts to accurately filter some data from a CSV file I am importing. I have attached both a sample of the CSV data and my script. The attached CSV file contains two rows and 27 columns of data. The first column is the station ID BLS, the second column is the sensor number 4, the third column is the date, and the remaining 24 columns are hourly temperature readings. In my attached script, I read in row[3:] to extract just the temperatures, do a sanity check to make sure there are 24 values, remove any missing or m values, and then append the non-missing values into the hour_list. Strangely the the first seven rows appear to be empty after reading into the CSV file, so that's what I had to incorporate the if len(temps) == 24 statement. But the real issue is that for days with no missing values, for example the second row of data, the length of the hour_list should be 24. My script, however, is returning 23. I think this is because the end-of-row-values have a trailing \. This must mark these numbers as non-digits and are lost in my isdig filter line. I've tried several ways to remove this trailing \, but to no success. Do you have any suggestions on how to fix this issue? Many thanks in advance, Neil Berg csv_test.py 1KViewDownload csv_sample.csv 1KViewDownload Hello Neil, I just had a quick look at your script. To remove the trailing \ you can use val = val.rstrip('\\') in your script. Note the double backslash. The script now returns 24 items in the hour_list. Good luck! Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing array from java to python
On Jun 2, 11:54 am, loial jldunn2...@gmail.com wrote: I need to pass some sort of array or hashmap from Java and read the data in a python script (which will be called by the java class). Is there any neater way to do this other than just passing strings? I recently had to deal with the same problem, some bi-directional communication between Java and Python. Several options were discussed between me and my fellow programmer. In the end we settled for XML- rpc. It works remarkably well in our case. We use it to pass test and simulation data to GUI code. XML-rpc is very well supported in python. Basic types (lists, dicts etc.) are encoded automatically. If the arrays are very large, I would probably use an intermediate database (e.g. Hdf5) for storage and then use some sort of messaging to inform the Java code of any changes. -- http://mail.python.org/mailman/listinfo/python-list
Re: code structure advise for a model
On Feb 4, 3:43 am, Martin De Kauwe mdeka...@gmail.com wrote: Hi, I am translating some c++ code to python and just wanted to ask some advise on structure. The original has everything declared globally and nothing passed via function (I assume, but don't know, that this isn't just standard c++ practice!). So given this, I have a pretty much clean slate as I can't quite just copy the functions over. I was thinking something like this class Params: def __init__(self, fname): self.set_inital_condtions() self.read_input_file(fname) def set_inital_conditons(self): self.some_parm = 0.0 def read_input_file(fname): #read file, change initial params if specified then I thought I could pass this as an object to the model class class Model(Params): def __init__(self): # blah def some_func(self): if (Params.some_param == something): foo OR this just a very bad way to structure it? The other thing I can't decide on is how to pass the parameters and variables through the class. So because of the way the original is written (everything is global), I could just inherit things, but it does means there is a lot of self. syntax. So I wondered if it might be better to pass things as function arguments? Any thoughts? I am also half considering other users from non-python backgrounds and what might seem very alien (syntax) to them. thanks in advance (ps. I am cross posting this on comp.lang.python as I am not sure where is more appropriate). I would structure it in three classes/functions: 1. A parser class/function that reads an input file and returns a Parameters object 2. A Parameters class 3. A Model class that uses the Parameters You would end up with something like the following: class MyParser(object): def __init__(self, filename=None): self.filename = filename def parse(self, filename): params = Parameters() ...read info from filename and update parameters return params class Parameters(object): def __init__(self): self.myAttribute1 = 0 self.myAttribute2 = A string class MyModel(object): def __init__(self, parameters): self.parameters = parameters def solve(self): ...solve the problem The driver program would look something like this: parser = MyParser() params = parser.parse('inputfile') model = MyModel(params) model.solve() I hope this is helpfull for you. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing floats
On 29 nov, 00:20, Nobody nob...@nowhere.com wrote: On Sat, 27 Nov 2010 18:23:48 -0500, Terry Reedy wrote: Therefore, to implement this multiplication operation I need to have a way to verify that the float tuples C and D are equal. I might try the average relative difference: sum(abs((i-j)/(i+j)) for i,j in zip(C,D))/n # assuming lengths constant The division is unstable if i and j are close to zero. For scalars, I'd use: abs(i-j) = epsilon * (1 + abs(i+j)) This amounts to a relative error check for large values and an absolute error check for values close to zero. For a vector, I'd check that the above holds for all pairs. Hi All, Why don't you treat the C and D tuples as vectors? You can than check dot product and norm (length) for equality. Using scipy (www.scipy.org), you even get very nice performance. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: fast kdtree tree implementation for python 3?
On 11 sep, 20:15, _wolf wolfgang.l...@gmail.com wrote: does anyone have a suggestion for a ready-to-go, fast kdtree implementation for python 3.1 and up, for nearest-neighbor searches? i used to use the one from numpy/scipy, but find it a pain to install for python 3. also, i'm trying to wrap the code fromhttp://code.google.com/p/kdtree/ using cython, but i'm still getting errors. i wish stuff like kdtree, levenshtein edit distance and similar things were available in the standard library. Do you know about the kdtree implementation in biopython? I don't know if it is already available for Python 3, but for me it worked fine in Python 2.X. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Processing HTML form
On 17 jun, 21:11, Bradley Hintze bradle...@aggiemail.usu.edu wrote: Hi, I am a newbie to anything web related, I know a bit of HTML though. I've been programing in python for a year or so so I know the language at an intermediate level. I am wondering if its possible to get info from an HTML form and pass it to my python code and return a page based on the code executed. All of my web searches have been fruitless as I quickly get lost in the jargon and perhaps not sure what phrase i should search. Any help would be appreciated. Thank you, Bradley Hi Bradley, If I understand correctly, you want to do the following: 1. Fill in a HTML form in a client (web browser) 2. Send the form to a webserver 3. Have the webserver extract the information from the form 4. Send the information to a python program for processing 5. Generate a new HTML page in python based on the information in step 4 6. Send the newly generated page back to the client Possible solutions depend a little on the constraints and/or options you have for the webserver. If you are free in your choices I would suggest to start with cherrypy (www.cherrypy.org) for a webserver. It is simple to start with, very powerfull and well documented. This would cover steps 3 and 4. For step 5 I suggest you either use the python builtin string template facility string.Template (for simple things). If you want more power have a look at Genshi http://genshi.edgewall.org. The two options I mention above help you to understand whats going on. If you feel comfortable with this, you can take a look at python web frameworks like Django or Turbogears (or many others). Good luck and keep us posted with what your progress. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert .doc to .pdf
On 14 jun, 13:19, Thales thales@gmail.com wrote: Good morning, I need to convert some files from .doc to .pdf. I've googled it a little bit and all the solutions I've found used the OpenOffice API, but I can't use it. Anybody knows a library that I can use to do it? Thanks What about using the win32 API and use the free PDFCreator (http:// sourceforge.net/projects/pdfcreator/) PDF printer? This should be very simple (code untested, but should be roughly OK): - Instantiate MS/Word application from win32com.client import Dispatch app = Dispatch('Word.Application') - Open your document doc = app.Documents.Open(demo.doc) - Print to PDF app.ActivePrinter = PDFCreator app.PrintOut() The PrintOut call is a little tricky. I normally try to decode and guess the Python call from the corresponding VisualBasic code I record with the macro recording facility. I know there are more sophisticated methods out there, but I never tried them. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert .doc to .pdf
On 14 jun, 17:55, Thales thales@gmail.com wrote: On 14 jun, 11:01, Marco Nawijn naw...@gmail.com wrote: On 14 jun, 13:19, Thales thales@gmail.com wrote: Good morning, I need to convert some files from .doc to .pdf. I've googled it a little bit and all the solutions I've found used the OpenOffice API, but I can't use it. Anybody knows a library that I can use to do it? Thanks What about using the win32 API and use the free PDFCreator (http:// sourceforge.net/projects/pdfcreator/) PDF printer? This should be very simple (code untested, but should be roughly OK): - Instantiate MS/Word application from win32com.client import Dispatch app = Dispatch('Word.Application') - Open your document doc = app.Documents.Open(demo.doc) - Print to PDF app.ActivePrinter = PDFCreator app.PrintOut() The PrintOut call is a little tricky. I normally try to decode and guess the Python call from the corresponding VisualBasic code I record with the macro recording facility. I know there are more sophisticated methods out there, but I never tried them. Regards, Marco Thanks for your help Marco, but it has to work on linux, not on windows. Is possible to import this win32com on linux systems? How? Thank you! Nope. The win32 related packages only work on Windows systems. Maybe Wine is an option? So running a genuine MS Office under Wine on Linux? Otherwise, your best bet is still OpenOffice (although you mention you cannot use it) Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: using reverse in list of tuples
On Jun 10, 2:39 am, james_027 cai.hai...@gmail.com wrote: hi, I am trying to reverse the order of my list of tuples and its is returning a None to me. Is the reverse() function not allow on list containing tuples? Thanks, James As the others already mentioned list.reverse() is in-place, just as for example list.sort(). Alternatively, use the builtin reversed() or sorted() functions to get a return value (they will leave the original list unmodified. Example: a = range(3) b = reversed(a) for item in b: ...print item This will produce: 2 1 0 Note that reversed returns an iterator. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Microsoft Office Word and Python (Win XP)
On Jan 9, 4:12 pm, 3lvss0...@gmail.com 3lvss0...@gmail.com wrote: Hi. Im very new with python. I have got some answer on my issue to use interop or COM ''plugins'' to access MS Word through python but i don't even know what those two ''plugins'' are so I cannot use them. What I want to do is the following: I need the script that moves (only moves, not change or delete!) entire (100% of the text) text from one .doc file to another. But its not so easy as it sounds. The target .doc file is not the only one but can be many of them. All the target .doc files are always in the same folder (same path) but all of them don't have the same name. The .doc file FROM where I want to move entire text is only one, always in the same folder (same path) and always with the same file name. Names of the target are only similar but as I have said before, not the same. Here is the point of whole script: Target .doc files have the names: HD1.doc HD2.doc HD3.doc HD4.doc and so on What I would like to have is moved the entire (but really all of the text, must be 100% all) text into the .doc file with the highest ( ! ) number. The target .doc files will always start with ''HD'' and always be similar to above examples. It is possible that the doc file (target file) is only one, so only HD1.doc. Therefore ''1'' is the maximum number and the text is moved into this file. Sometimes the target file is empty but usually won't be. If it won't be then the text should be moved to the end of the text, into first new line (no empty lines inbetween). So for example in the target file which has the maximum number in its name is the following text: a b c In the file from which I want to move the text is: d This means I need in the target file this: a b c d Could someone tell me please how to do this? Thank you. Hi, I will try to head you in the right direction with the Python/MS.Word link. First of all, you need to install the win32 extension. See http://sourceforge.net/projects/pywin32/ Once you have this installed you can instantiate a MS.Word application like this (code untested): from win32com.client import Dispatch app = Dispatch(Word.Application) app.Visible = True The code so-far is more or less equivalent to opening Word without opening a document (normally Word will start with an empty document). To open a document do something like the following. doc = app.Documents.Open(c:\\example.doc) Further builtin Python modules that could be helpfull are: glob- for searching files matching a pattern os, os.path - for path related functionality like stripping directory names from a complete path Take a look at the online documentation for more information http://docs.python.org/modindex.html Good luck and let us know the result. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Microsoft Office Word and Python (Win XP)
On Jan 9, 8:18 pm, 3lvss0...@gmail.com 3lvss0...@gmail.com wrote: Marco Nawijn: I have had installed pywin32 already. The three lines that you mentoined don't do this I checked at my own computer and it works fine. also what did you mean with doc = app.Documents.Open(c:\\example.doc). Which document should I open with this line? This was just meant as an example on how to open a Word document from within python. This would be the basis for copying/appending from your source document to your target document (e.g. HD10.doc). It shouldn't be opened anything. I was asking about the script as automated process. If you know how could i do this? Well it is a python script. So you can run it as an automated process. You should just set app.Visible=False so the Word user interface component is not shown. Terry Reedy: I have never mentoined copying files but moving the whole text from, always the same (same name, same path), .doc file with. However copying (=moving) text to correct .doc file would be good yes. I know command prompt and its copy function but this way it wouldn't work because I would have to define the target file - the file INTO which I want to move the text. But I will never know the file name (target file). The only thing I know is: - the file is one of .doc files that start with HD - whole name of those .doc file is always HDX.doc where X is a number and I need to move the text into the file with maximum X (the most high number) - all the HD files will be always in the same path (same folder) but I would like to use the path inside the code (it might be obvious that I have to) because on PC and laptop, I have two different usernames and since HD files are located inside Documents And Settings, I have to use two copies of the script - one for PC, one for laptop. Dennis Lee Bieber: Im not familiar with python, also Im not programmer. Thats why Im not able to do so when people tell me do this then use XYZ function which will give you ZYX from what you can do that and you will get result. Im still willing to learn but there are thousands of python tutorials and the one for exsactly this topic probably doesn't exsist. The .doc extension is required, so I cannot use .txt because I need the HD files in .doc. -- http://mail.python.org/mailman/listinfo/python-list
Re: please help shrink this each_with_index() implementation
On Jan 5, 8:58 pm, Phlip phlip2...@gmail.com wrote: Hypo Nt: def each_with_index(seq): index = 0 result = [] for item in seq: result.append([item, index]) index += 1 return result My Pythonic sequencing skills are obviously feeble. Can anything think of a way to write that in fewer lines? -- Phlip http://c2.com/cgi/wiki?MoreliaViridis You could use the build-in function enumerate inside a list comprehension. seq = range(5) [ (i,s) for i,s in enumerate(seq) ] [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] This will reduce the function to a one-liner. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: problems trying to build python 2.6 as a shared library
On Sep 25, 1:08 pm, Chris Withers ch...@simplistix.co.uk wrote: Hi All, I'm trying to build Python 2.6 as a shared library, so I did: ./configure --enable-shared make make altinstall No obvious signs of failure, but when I try and use the resulting python, I get: python2.6: error while loading shared libraries: libpython2.6.so.1.0: cannot open shared object file: No such file or directory Why might that be? cheers, Chris -- Simplistix - Content Management, Batch Processing Python Consulting -http://www.simplistix.co.uk Hello Chris, The dynamic loader cannot find the python shared library. There are at least 2 options: 1. Add path that contains the shared library to the LD_LIBRARY_PATH environment variable. In a bash shell this can be accomplished by: export LD_LIBRARY_PATH=/path/to/python_shared_lib: $LD_LIBRARY_PATH 2. Add path to dynamic linker configuration file. This typically is in '/etc/ld.so.conf'. See man page for ld for more information. Note that I assumed that you are on a Unix/Linux machine. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Another form of dynamic import
Hello, In short I would like to know if somebody knows if it is possible to re-execute a statement that raised an exception? I will explain the reason by providing a small introduction on why this might be nice in my case and some example code. I am using the python bindings to a *very* large C++ library. About 5000 classes divided over approx. 450 different packages are exposed through the Python interface. To reduce the number of import statements that need to be inserted and to limit the number of wildcard imports it would be very helpful if class names could be automatically imported from the proper module. There is no problem in finding out the proper module given a (valid) class name. As an example, look at the following statement aPoint = gp_Pnt(1.0, 0.0, 0.0) # Oops, this will raise a NameError, since # gp_Pnt class is unknown NameError: name 'gp_Pnt' is not defined As indicated, this will raise a NameError exception. What I would like to do is something like the following (pseudo-code): try: aPoint = gp_Pnt(1.0, 0.0, 0.0)[1] except NameError, e: name = e.args[0].split[1] if isValid(name): doImport(name) === Can I go back to statement [1] from this point? else: raise e There is no problem in catching the exception, finding out which name is unknown to python and check if this is a valid name for my library. My question is, is there any possibility of going back to the statement that raised the error, re-execute the statement and continue? Thanks for any thoughts and suggestions. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: What would you like to see in a book about Matplotlib?
On Jan 5, 10:57 am, Sandro Tosi matrixh...@gmail.com wrote: Hello and Happy 2009! I received the interesting proposal to author a book on Matplotlib, the powerful 2D plotting library for Python. While preparing the arguments list, I'd like to hear even your opinion, because different points-of-view will lead to a better product. Some basic question I'd like to ask are: - what are you using matplotlib for? - what are the things you like the most of matplotlib, that you want to give emphasis to? And why? - what are the (basic) things that, when you were beginning to use matplotlib, you wanted to see grouped up but couldn't find? - what would you like to see in a book about matplotlib? - what are some those advanced feature that made you yell WOW!! ? - what are the things you'd like to explore of matplotlib and never had time to do? Your suggestions are really appreciated :) And wish me good luck! Hello Sandro, I am happy to hear that there might be a book on Matplotlib. I am using Matplotlib for a while now and find it a very usefull and powerfull library for generating graphs. I will try to answer some of your questions. I am by no means an expert in the field, so I only express my personal experience. What are you using matplotlib for? I currently use Matplotlib to generate a series of graphs that visualise data channels (strain measurements) during a structural test. These graphs need to be refreshed about every 4 seconds. Each of the graphs contains 1 to 6 curves. What are the things you like the most of matplotlib, that you want to give emphasis to? And why? In general I like the most the fact that I can generate high quality publication ready graphs with a consistent look with minimal effort. Further, although the matlab-style commands are very usefull I think the class interface is more powerfull. The class interface also has a steeper learning curve. So, for me it would be very interesting if much emphasis will be on the class interface. What are the (basic) things that, when you were beginning to use matplotlib, you wanted to see grouped up but couldn't find? I seriously hope the book is not for dummies, but provides at least a significant amount of information on more advanced features of Matplotlib. I think the currently available tutorials are sufficient to getting started. From my point of view I am very interested in: - Dynamically updating a series of graphs - Configuring layout of graphs (colors, labels, legends, etc.) - Managing dimensions of the figure when you export the figure to e.g PNG. For me this is interesting in the sense that if I insert a figure in a report/paper, I would like that the fonts used in the graph remain proportional to the surrounding text. If you need more information please let me know. I wish you good luck! Kind regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: Display the results of a query to an html table
On Aug 19, 4:35 pm, Amie [EMAIL PROTECTED] wrote: Hi, how do you display the results of an sql query and display it onto the html form or html table Thanks Hello, You might want to take a look at: sqlobject http://www.sqlobject.org/ or sqlalchemy http://www.sqlalchemy.org/ These can assist in translating SQL query results into Python objects. For generating the html forms and tables out of the sql results data you can try: genshi: http://genshi.edgewall.org/ kid: http://www.kid-templating.org/ I hope this helps. Marco -- http://mail.python.org/mailman/listinfo/python-list
Re: easy 3D graphics for rendering geometry?
On Nov 8, 6:53 am, gsal [EMAIL PROTECTED] wrote: What would be the easiest way to go about offering 3D graphics for the purpose of rendering geometry? Suppose engineers (my co-workes) have to design some enclosure, nozzle, bracket, or whatever physical part/component, I would like to write a program where they can at least see the resulting geometry and navigate it, i.e., zoon-in/out, rotate, pan. On the side, I could have data entry fields with the input parameters and when something is changed, the graphics can be updated immediately (after the necessary calculations have been done). I know I need to learn something, and I am willing, I just need help choosing what to learn. I don't have any experience on this matter, don't know OpenGL, Mesa, VTK, VRS, Maya...and all seem to have a steep learning curve. I don't know any of the other graphics packages more oriented for game/ scenery/movie development (Panda, etc.), either. I do know my trig and build my FEA parts parametrically from points, to line, to surfaces, to volumes or from volume boolean algebra. I would like the choice to be some kind of module/API that works equally well on Linux as in Windows. So: What would be the easiest way? and would it be worth learning? or is it better to shoot for something not so easy but worth learning? thanks in advance for any pointers. gsal Hello, Take a look at www.opencascade.org. This is a powerfull C++ library for building CAE (Computer Aided Engineering) applications. It also has a rather steep learning curve, but the resulting geometry could be easily exported to FEA packages. Another possibility is www.salome-platform.org which is build on top of OpenCascade. It has a nice Python interface which makes the learning curve probably a little more acceptable. A downside is that, I think there are no Windows binaries yet. Regards, Marco Nawijn -- http://mail.python.org/mailman/listinfo/python-list
Code design problem
Hello, I have a hard time figuring out an elegant and efficient design for the following problem. I am working on automation of structural design problems. In the majority of cases, this boils down to executing programs in batch in one or more loops. The scripts to control the execution differ from fortran to bash to python and so on. Most of them are ad hoc and what I call 'throw away scripts'. In order to improve the situation I would like to develop a Python module that supports the execution of external programs. Ideally I would like to make running locally or remote trivial for the users of the module. As an example, I would like the following (pseudo)-code to work: app = Application('patran')# Run on local machine app.start(args) app = Application('patran', host='myhost') # Run on remote machine app.start(args) The problem I face is that the implementation of the application class is completely different for the local and remote case. The local case is a straightforward implemenation using the subprocess module, the remote case is a CORBA implementation. Somehow I would like to switch from implementation class at runtime depending on whether or not the host parameter is specified or not. The Application, local implementation and remote implementation all have the same interface, so a possibility might be something like the following: class Interface(object): . def start(self): pass def stop(self): pass class LocalImplementation(Interface): . class GlobalImplementation(CorbaGlobalImplementation, Interface): . class Application(Interface): def __init__(self, program, host=None): if host: self.__impl = LocalImplementation(program) else: self.__impl = GlobalImplementation(program, host) # Forward all methods to the implementation class def start(self): self.__impl.start() def stop(self): self.__impl.stop() To me forwarding each call in the Application class looks a little bit redundant and I would like to get rid of it. Does anyone have any comments or suggestions? Can metaclass programming come to rescue? Kind regards, Marco Nawijn -- http://mail.python.org/mailman/listinfo/python-list
Re: Code design problem
On Aug 29, 3:03 pm, Marshall T. Vandegrift [EMAIL PROTECTED] wrote: Marco Nawijn [EMAIL PROTECTED] writes: The problem I face is that the implementation of the application class is completely different for the local and remote case. The local case is a straightforward implemenation using the subprocess module, the remote case is a CORBA implementation. Somehow I would like to switch from implementation class at runtime depending on whether or not the host parameter is specified or not. The Application, local implementation and remote implementation all have the same interface, so a possibility might be something like the following: snipped example To me forwarding each call in the Application class looks a little bit redundant and I would like to get rid of it. Does anyone have any comments or suggestions? Can metaclass programming come to rescue? It sounds like you could probably get away with just a factory function: def Application(program, host=None): if host is None: return LocalApplication(program) else: return RemoteApplication(program, host) Then just implement the same interface and/or derive from a common base class for LocalApplication and RemoteApplication. HTH!, -Marshall Thanks! This makes perfect sense. (In these moments I always wonder why I didn't come up with this idea...) Thanks again. Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list