create PyString


in an extension module i'd like to create a very large PyString. As the string is very large i'd first like to create the string and let Python allocate the space for it and then fill it from my ...
Posted On: Sunday 25th of November 2012 09:04:50 PM Total Views:  85
View Complete with Replies

RELATED TOPICS OF Python Programming PROGRAMMING LANGUAGE




py2exe - change name of exe created

I have a setup script like this; setup(windows = [{"script":"myprogram.py", "icon_resources":[(0,"nabbitt.ico")], "other_resources": [(24,1,manifest)]} ], name = "Nabbitt ver 0.1", data_files = [("",rootdata)], zipfile = None, options = {"py2exe": { "compressed" : 1, "dll_excludes": ["w9xpopen.exe"], #should also exclude MSVCR71.dll "bundle_files": 3 # 1=Single .exe, 2=.exe with pythonXX.dll } }, )
VIEWS ON THIS POST

203

Posted on:

Wednesday 7th November 2012
View Replies!

cannot create my own dict

all, This morning I tried to create my own read-only dictionary, and failed miserably. I don't understand why, can somebody enlighten me Below is a brute-force experiment that cannot deal with "x in obj", plz read the explanation below the code: -------------------------------- class myowndict(object): def __init__(self, mydict): self.mydict = mydict # Below is produced with # print '\n'.join([' self.%s = self.mydict.%s' % (v,v) # for v in dir(dict)]) # commented-out functions done by hand # #self.__class__ = self.mydict.__class__ self.__cmp__ = self.mydict.__cmp__ self.__contains__ = self.mydict.__contains__ self.__delattr__ = self.mydict.__delattr__ self.__delitem__ = self.mydict.__delitem__ #self.__doc__ = self.mydict.__doc__ self.__eq__ = self.mydict.__eq__ self.__ge__ = self.mydict.__ge__ self.__getattribute__ = self.mydict.__getattribute__ self.__getitem__ = self.mydict.__getitem__ self.__gt__ = self.mydict.__gt__ self.__hash__ = self.mydict.__hash__ #self.__init__ = self.mydict.__init__ self.__iter__ = self.mydict.__iter__ self.__le__ = self.mydict.__le__ self.__len__ = self.mydict.__len__ self.__lt__ = self.mydict.__lt__ self.__ne__ = self.mydict.__ne__ #self.__new__ = self.mydict.__new__ self.__reduce__ = self.mydict.__reduce__ self.__reduce_ex__ = self.mydict.__reduce_ex__ self.__repr__ = self.mydict.__repr__ self.__setattr__ = self.mydict.__setattr__ self.__setitem__ = self.mydict.__setitem__ self.__str__ = self.mydict.__str__ self.clear = self.mydict.clear self.copy = self.mydict.copy self.fromkeys = self.mydict.fromkeys self.get = self.mydict.get self.has_key = self.mydict.has_key self.items = self.mydict.items self.iteritems = self.mydict.iteritems self.iterkeys = self.mydict.iterkeys self.itervalues = self.mydict.itervalues self.keys = self.mydict.keys self.pop = self.mydict.pop self.popitem = self.mydict.popitem self.setdefault = self.mydict.setdefault self.update = self.mydict.update self.values = self.mydict.values # end of __init__ if __name__ == '__main__': fd = myowndict({1:10}) print 1 in fd # FAILS! (with "TypeError: iterable argument required") -------------------------------- I wanted to make my own dictionary. However, a simple element test failed (after implementing various __*__ functions), and I cannot figure out why. The above code is a brute force attempt, where I forward all methods (except __class__, __doc__, __init__, and __new__) to my local 'mydict' object. IT STILL FAILS. So if copying all methods of a native dictionary is not enough, what should I do to make my class work as a dictionary WITHOUT deriving from dict (which will obviously work). Sincerely, Albert
VIEWS ON THIS POST

251

Posted on:

Wednesday 7th November 2012
View Replies!

Python doesn't see the directories I create

When I create a subfolder, python is not seeing it. Can someone please explain this behaviour I just started with python, read the tutorial over the weekend and am writing my very first script. So I may not be seeing something. Both os.path and glob.glob seem not to see a folder I created. Other sibling folders seem to work fine. On a whim I tried paths with \\ double slashes and that worked. But why should single slashes work for some folders and not for others \t What I need is to match a bunch of files in a folder with glob.glob(C:\enhancement\rawfiles\*.bin.gz) and send them to a function that unzips them. But I always get []. Yes the folder does have my files, and I checked all permissions etc, everything looks identical to the other folders which python IS seeing. Same problem on my vista pc too. Behaviour reproduced below. C:\Enhancement>dir /ad Volume in drive C has no label. Volume Serial Number is 8056-41E7 Directory of C:\Enhancement 08/28/2007 06:15 PM . 08/28/2007 06:15 PM .. 08/28/2007 06:07 PM BSA Documentation 08/28/2007 05:56 PM output 08/29/2007 07:08 PM rawfiles 08/23/2007 04:38 PM SnapCell2.3.2 08/28/2007 06:15 PM test 0 File(s) 0 bytes 7 Dir(s) 35,703,283,712 bytes free C:\>python -V Python 2.5.1 C:\>python -c "import os; print os.path.exists('C:\enhancement\output')" True C:\>python -c "import os; print os.path.exists('C:\enhancement\rawfiles')" False C:\>python -c "import os; print os.path.exists('C:\\enhancement\\rawfiles')" True C:\>python -c "import glob; print glob.glob('C:\enhancement\rawfiles\*')" [] C:\>python -c "import os; print os.path.exists('C:\enhancement\test')" False C:\>python -c "import os; print os.path.exists('C:\enhancement\snapcell2.3.2')" True , Bruno Desthuilliers wrote: > mr_gadget a crit : >> When I create a subfolder, python is not seeing it. Can someone please >> explain this behaviour I just started with python, read the tutorial over >> the weekend and am writing my very first script. So I may not be seeing >> something. Both os.path and glob.glob seem not to see a folder I created. >> Other sibling folders seem to work fine. On a whim I tried paths with \\ >> double slashes and that worked. But why should single slashes work for some >> folders and not for others \t > Note you *didn't* try paths with double slashes, you merely correctly represented the paths with single slashes :-) > s/slash/antislash/g > > It's a very well known gotcha due to MS's choice to use the antislash as > path separator. In most languages - Python included - the antislash is > used for escape sequences (non-printable characters). '\r' is the escape > sequence for CR (carriage return). Doubling the antislash prevents > escaping. > > You can avoid all escaping by using raw strings: > > mypath = r"C:\enhancement\rawfiles\" > Please note that the above is a well-known syntax error. A string literal cannot end with a single backslash, as it escapes the closing quote. >>> mypath = r"C:\enhancement\rawfiles\" File "", line 1 mypath = r"C:\enhancement\rawfiles\" ^ SyntaxError: EOL while scanning single-quoted string >>> > Also and IIRC, using slash instead should also work, ie: > > mypath = r"C:/enhancement/rawfiles/" > That does indeed work in most situations, but ideally (i.e. for maximum code portability) paths should be constructed using os.path.join(), or collected from the environment somehow. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden --------------- Asciimercial ------------------ Get on the web: Blog, lens and tag the Internet Many services currently offer free registration ----------- Thank You for Reading -------------
VIEWS ON THIS POST

120

Posted on:

Wednesday 7th November 2012
View Replies!

How to create a single executable of a Python program

Dear fellows, I'm trying to create a executable file using py2exe . Unfortunately along with the python executable file it also creates some other files that are needed in order to the executable be able to run in a system that doesn't have Python installed. Can some one guide me on how can I merge all this files created by py2exe in a single exe file If I have a python program that uses an image file I don't want this image file to be exposed in the folder but only to be accessible through the program flow..
VIEWS ON THIS POST

230

Posted on:

Wednesday 7th November 2012
View Replies!

How to code dynamically created methods?

I've tried a bazillion ways to code dynamically generated methods, to no avail. The following snippet is a very simplified (and artificial) demo of the problem I'm running into, featuring my latest attempt at this. The idea here is to use __getattr__ to trap any attempt to invoke a nonexistent method, have it return a generic handler called _auto which creates the new method dynamically, invokes it, and "installs" it in the class, so that subsequent calls to the same method go directly to the newly created method, instead of to __getattr__. It is this last step, the "installation" of the new method, that is giving me problems. class A( object ): def __getattr__( self, name ): self._auto_name = name return self._auto def hello( self, name ): print "hi! my name is %s" % name def _auto( self, *args ): name = self._auto_name def m( self, *args ): self.hello( name ) m( self, *args ) m = classmethod( m ) setattr( A, name, m ) x = A() x.foo() # ok x.foo() # bombs >>> reload(test) hi! my name is foo Traceback (most recent call last): File "", line 1, in File "test.py", line 19, in x.foo() File "test.py", line 12, in m self.hello( name ) TypeError: unbound method hello() must be called with A instance as first argument (got str instance instead) >>> I'm sure that the problem is with my naive attempt to add a method to class A dynamically (in the last two lines of the definition of _auto). What's the right way to do this
VIEWS ON THIS POST

273

Posted on:

Wednesday 7th November 2012
View Replies!

How to create a tuple quickly with list comprehension?

Hi all, I can use list comprehension to create list quickly. So I expected that I can created tuple quickly with the same syntax. But I found that the same syntax will get a generator, not a tuple. Here is my example: In [147]: a = (i for i in range(10)) In [148]: b = [i for i in range(10)] In [149]: type(a) Out[149]: In [150]: type(b) Out[150]: Is there a way to create a tuple like (1, 2, 3, 4, 5, 6, 7, 8, 9) quickly I already I can use tuple() on a list which is created by list comprehension to get a deed tuple.
VIEWS ON THIS POST

185

Posted on:

Wednesday 7th November 2012
View Replies!

Can python create a dictionary from a list comprehension?

I'm trying to turn o list of objects into a dictionary using a list comprehension. Something like entries = {} [entries[int(d.date.strftime('%m'))] = d.id] for d in links] I keep getting errors when I try to do it. Is it possible Do dictionary objects have a method equivalent to [].append Maybe a lambda
VIEWS ON THIS POST

107

Posted on:

Wednesday 7th November 2012
View Replies!

PPY 0.0.5 -- create Python callables in PPC assembly

More-or-less for my own amusement, I've written a small Python package that allows one to write Python callables in PowerPC assembly: http://starship.python.net/crew/mwh/hacks/PPY.html This is clearly an insane thing to want to do, but take a look if it sounds intriguing. Currently only supports Mac OS X but I imagine a port to linux/ppc would be fairly straightforward. Cheers, mwh
VIEWS ON THIS POST

103

Posted on:

Wednesday 7th November 2012
View Replies!

Re: How do I create user-defined warnings?

2008/6/18 Clay Hobbs : > I already know how to make user-defined exceptions, like this one: > > class MyException(Exception): > pass > > But for a module I'm making, I would like to make a warning (so it just > prints the warning to stderr and doesn't crash the program). I have > tried this: > > class MyWarning(Warning): > pass > > And it behaves like a normal error. Please help me, I can't figure out > what I'm doing wrong. Use 'warnings' module. http://docs.python.org/lib/module-warnings.html > > -- > Ratfink > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Wbr, Andrii Mishkovskyi. He's got a heart of a little child, and he keeps it in a jar on his desk.
VIEWS ON THIS POST

118

Posted on:

Saturday 10th November 2012
View Replies!

best way to create a timer

I am wondering what is the best way to create a timer, like an alarm, once it reaches a time, it triggers an event. I have a way of doing this but it seems like it isn't good at all. If it helps at all I am using a Tkinter, but that probably doesn't mean much. The way I was doing it was using a while loop, and just saying while current time is not = to trigger time, do nothing, and when it is, do event. -- View this message in context: http://www.nabble.com/best-way-to-cr...p17815502.html Sent from the Python - python-list mailing list archive at Nabble.com.
VIEWS ON THIS POST

112

Posted on:

Saturday 10th November 2012
View Replies!

It is not possible to create a recursive function over a pyGTK treeStore

, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing all the files and folders so that it would map the file system hierarchy. I wrote a recursive function that would go through the file system tree. My problem is that a sub-node of a gtk.TreeStore is not a gtk.TreeStore, but a gtk.TreeIter. And gtk.treeter has almost no functions I would be able to use (like "append") Therefore I can not explore the whole file system. Do you have another way so that I would be able to put all the file system hierarchy into a gtk.TreeStore I was quite happy with my recursive solution ... =================================== code : #!/usr/bin/env python import pygtk pygtk.require('2.0') import gtk, gobject, os prefdir="/tmp/a" class InfoModele: def matchPathTreetoModelTree(self, path, treeStoreNode): files = [f for f in os.listdir(path) if f[0] '.'] for i in range(len(files)): if os.path.isfile(path+'/'+files[i]): print path+'/'+files[i]+" is a file" treeStoreNode.append( None, (path+'/'+files[i], None) ) if os.path.isdir(path+'/'+files[i]): #is a directory, go recursively print path+'/'+files[i]+" is a directory" mother = self.tree_store.append( None, (path+'/'+files[i], None) ) self.matchPathTreetoModelTree(path+'/'+files[i], mother) def __init__(self): self.tree_store = gtk.TreeStore( gobject.TYPE_STRING, gobject.TYPE_STRING) path=prefdir self.matchPathTreetoModelTree(prefdir, self.tree_store) if __name__ == "__main__": i=InfoModele() ================================= file system : a |-ab/ |-abc/ |-abcd ================================ Program output : /tmp/a/ab is a directory /tmp/a/ab/abc is a file Traceback (most recent call last): File "question.py", line 28, in i=InfoModele() File "question.py", line 25, in __init__ self.matchPathTreetoModelTree(prefdir, self.tree_store) File "question.py", line 20, in matchPathTreetoModelTree self.matchPathTreetoModelTree(path+'/'+files[i], mother) File "question.py", line 16, in matchPathTreetoModelTree treeStoreNode.append( None, (path+'/'+files[i], None) ) AttributeError: 'gtk.TreeIter' object has no attribute 'append'
VIEWS ON THIS POST

102

Posted on:

Saturday 10th November 2012
View Replies!

Creating object in function doesn't seem to create a new object.

Hi folks, I'll start off with the code I wrote... (ActivePython 2.4 on Windows XP SP2) ------------------------------- class FlightCondition(object): lsf = [0,'Low Speed Flare'] vto = [0,'Vertical Take-Off'] def get_flight_condition(flight_data): fc1 = FlightCondition() for row in flight_data: fc1.lsf[0] += 1 fc1.vto[0] += 1 print 'in function get_flight_condition' print fc1.lsf print fc1.vto return fc1 for count in range(3): fc = get_flight_condition([1,2,3]) print 'returned fc' print fc.lsf print fc.vto --------------------------------- When I run it I get... in function get_flight_condition [3, 'Low Speed Flare'] [3, 'Vertical Take-Off'] returned fc [3, 'Low Speed Flare'] [3, 'Vertical Take-Off'] in function get_flight_condition [6, 'Low Speed Flare'] [6, 'Vertical Take-Off'] returned fc [6, 'Low Speed Flare'] [6, 'Vertical Take-Off'] in function get_flight_condition [9, 'Low Speed Flare'] [9, 'Vertical Take-Off'] returned fc [9, 'Low Speed Flare'] [9, 'Vertical Take-Off'] --------------------------------- I thought that when I wrote fc1 = FlightCondition() in the function it would create a new FlightCondition object which would be passed back every time. Instead it seems to re-reference the old version and continue to add to it. --------------------------------- What I expected was... in function get_flight_condition [3, 'Low Speed Flare'] [3, 'Vertical Take-Off'] returned fc [3, 'Low Speed Flare'] [3, 'Vertical Take-Off'] in function get_flight_condition [3, 'Low Speed Flare'] [3, 'Vertical Take-Off'] returned fc [3, 'Low Speed Flare'] [3, 'Vertical Take-Off'] in function get_flight_condition [3, 'Low Speed Flare'] [3, 'Vertical Take-Off'] returned fc [3, 'Low Speed Flare'] [3, 'Vertical Take-Off'] --------------------------------- Could someone please explain to me why I get the output I did instead of what I expected. How do I code my function so I get a new fc1 every time and my deed output
VIEWS ON THIS POST

101

Posted on:

Saturday 10th November 2012
View Replies!

gthumpy: create static HTML slide shows

Gthumpy is small collection of scripts for handling images from digital cameras. One script creates thumbnails, one script lets you enter metadata for every picture (date, description, title) in a GUI and an other creates static html files. The created ...
VIEWS ON THIS POST

129

Posted on:

Saturday 10th November 2012
View Replies!

create instance attributes for every method argument

I remember reading somewhere how to create an instance attribute for every method argument, but although Google is my friend, I can't seem to find it. This could likely be done way more elegant: ========================= class Test(object): def __init__(self, a, ...
VIEWS ON THIS POST

137

Posted on:

Sunday 11th November 2012
View Replies!

method to create class property

I like C#'s style of defining a property in one place. Can the following way to create a property be considered reasonable Python style (without the print statements, of course) class sample(object): def __init__(self): sample.y = self._property_y() def _property_y(self): ...
VIEWS ON THIS POST

189

Posted on:

Sunday 11th November 2012
View Replies!

How to create Python object in C/C++ extension by class name?

I'm trying to write a C++ extension like this: //----------------------------------------------------------------------- // C++ code class ActorBase {...}; // export ActorBase to Python as a new type class ActorManager { void addNewActor(const char* actorClassName) { \t\t } } // export ActorManagerto ...
VIEWS ON THIS POST

172

Posted on:

Sunday 11th November 2012
View Replies!

Python modules - how to create & which are better

I have embedded Python in my C++ application & creating Python function from the expression. I am then evaluating those Python compiled function (byte code) using PyObject_CallObject. I want to create a Python module which will have functions called by ...
VIEWS ON THIS POST

122

Posted on:

Monday 12th November 2012
View Replies!

Re: Cancel instance create

What about no Constructor , and a custom instancing function that can return either None or the instance wanted...
VIEWS ON THIS POST

96

Posted on:

Sunday 25th November 2012
View Replies!

Re: Cancel instance create

Fredrik Lundh wrote: > Aigars Aigars wrote: > >> I want MyClass to perform some tests and if them fail, I do not want >> instance to be created. If you do not want the instance created at all, you ...
VIEWS ON THIS POST

119

Posted on:

Sunday 25th November 2012
View Replies!

how to create GUI dynamically

Hi; i m working on a project where i need run time creation of GUI. i have some no. of entities for which i want checkboxes in front of them which can be checked/ unchecked by user. But the problem ...
VIEWS ON THIS POST

128

Posted on:

Sunday 25th November 2012
View Replies!