Re: Discussion on some Code Issues
On Sun, Jul 8, 2012 at 3:42 PM, wrote: > Thanks for pointing out the mistakes. Your points are right. So I am trying > to revise it, > > file_open=open("/python32/doc1.txt","r") > for line in file_open: > line_word=line.split() > print (line_word) Yep. I'd be inclined to rename file_open to something that says what the file _is_, and you may want to look into the 'with' statement to guarantee timely closure of the file, but that's a way to do it. Also, as has already been mentioned: keeping your data files in the Python binaries directory isn't usually a good idea. More common to keep them in the same directory as your script, which would mean that you don't need a path on it at all. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: how to compile pygtk in python2.7?
it's so strange when i input hisroty 506 cd /home/tiger/pygtk-2.24.0 507 PYTHON=/usr/local/bin/python2.7 ./configure --prefix=/usr 508 echo $? #i get 0 ,it means success?? 509 make 510 echo $? #i get 0 ,it means success?? 511 make install 512 echo $?#i get 0 ,it means success?? no wrong output can see . root@ocean:~# python2.7 Python 2.7.3 (default, Jul 1 2012, 14:13:18) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import gtk Traceback (most recent call last): File "", line 1, in ImportError: No module named gtk >>> 2012/7/8 Chris Angelico > On Sun, Jul 8, 2012 at 2:47 PM, contro opinion > wrote: > > 3.PYTHON=/usr/bin/python2.7 ./configure --prefix=/usr > > 4. make > > 5. make install > > What happened when you typed these commands? Were there failure > messages? As Benjamine suggested, do you need to become root to > install? > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: how to compile pygtk in python2.7?
On Sun, Jul 8, 2012 at 6:26 PM, contro opinion wrote: > it's so strange > when i input > hisroty > > 506 cd /home/tiger/pygtk-2.24.0 > 507 PYTHON=/usr/local/bin/python2.7 ./configure --prefix=/usr > 508 echo $? #i get 0 ,it means success?? > 509 make > 510 echo $? #i get 0 ,it means success?? > 511 make install > 512 echo $?#i get 0 ,it means success?? > > no wrong output can see . Any output at all? Normally configure and make will produce long screeds. If they didn't, that's wrong output. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Socket code not executing properly in a thread (Windows)
"John Nagle" wrote in message news:jta1v7$v0b$1...@dont-email.me... >We have no idea what IRC module you're using. I am sending plain text to the socket, sorry for not mentioning it before. At the simplest level, it appears that when the code runs in Windows, data is not sent to a socket when inside a thread, while on OS X it is. Also, running the sned to socket code inside the main loop would not work, as the main loop pauses until it recieves data from the socket (an IRC message), which is why I didn't put the SMS code in the main loop in the first place. > The Google Voice code you have probably won't work once you have > enough messages stored that Google Voice returns them on multiple > pages. I know that this is not the problem because the code works fine on OS X, but not Windows, where I actually need it to run. In addition, each message is deleted as it is parsed and no other SMSs are sent to the number, so fill-up should not be a problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to compile pygtk in python2.7?
1.PYTHON=/usr/local/bin/python2.7 ./configure --prefix=/usr the output is : checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /bin/mkdir -p checking for gawk... no checking for mawk... mawk checking whether make sets $(MAKE)... yes checking for style of include used by make... GNU checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables... checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking dependency style of gcc... gcc3 checking for bind_textdomain_codeset... yes checking build system type... i686-pc-linux-gnu checking host system type... i686-pc-linux-gnu checking for some Win32 platform... no checking for native Win32... no checking for a sed that does not truncate output... /bin/sed checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for fgrep... /bin/grep -F checking for ld used by gcc... /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld... yes checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B checking the name lister (/usr/bin/nm -B) interface... BSD nm checking whether ln -s works... yes checking the maximum length of command line arguments... 1572864 checking whether the shell understands some XSI constructs... yes checking whether the shell understands "+="... yes checking for /usr/bin/ld option to reload object files... -r checking for objdump... objdump checking how to recognize dependent libraries... pass_all checking for ar... ar checking for strip... strip checking for ranlib... ranlib checking command to parse /usr/bin/nm -B output from gcc object... ok checking how to run the C preprocessor... gcc -E checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking for dlfcn.h... yes checking for objdir... .libs checking if gcc supports -fno-rtti -fno-exceptions... no checking for gcc option to produce PIC... -fPIC -DPIC checking if gcc PIC flag -fPIC -DPIC works... yes checking if gcc static flag -static works... yes checking if gcc supports -c -o file.o... yes checking if gcc supports -c -o file.o... (cached) yes checking whether the gcc linker (/usr/bin/ld) supports shared libraries... yes checking whether -lc should be explicitly linked in... no checking dynamic linker characteristics... GNU/Linux ld.so checking how to hardcode library paths into programs... immediate checking whether stripping libraries is possible... yes checking if libtool supports shared libraries... yes checking whether to build shared libraries... yes checking whether to build static libraries... no configure: creating ./config.lt config.lt: creating libtool checking whether /usr/local/bin/python2.7 version >= 2.3.5... yes checking for /usr/local/bin/python2.7 version... 2.7 checking for /usr/local/bin/python2.7 platform... linux2 checking for /usr/local/bin/python2.7 script directory... ${prefix}/lib/python2.7/site-packages checking for /usr/local/bin/python2.7 extension module directory... ${exec_prefix}/lib/python2.7/site-packages checking for headers required to compile python extensions... found checking for PySignal_SetWakeupFd in Python.h... yes checking for python module thread... yes checking whether to enable threading in pygtk... yes checking for pkg-config... /usr/bin/pkg-config checking pkg-config is at least version 0.16... yes checking for GLIB - version >= 2.8.0... yes (version 2.24.2) checking for PYGOBJECT... yes checking for gio-types.defs... /usr/share/pygobject/2.0/defs/gio-types.defs checking for ATK... yes checking for PANGO... yes checking for codegen... /usr/share/pygobject/2.0/codegen checking for PYCAIRO... yes checking for PANGOCAIRO... yes checking for GTK... yes checking for GTK210... yes checking for GTK212... yes checking for GTK214... yes checking for GTK216... yes checking for GTK218... yes checking for GTK220... yes checking for GTK222... no no checking for GTK224... no no checking for LIBGLADE... yes checking for GTKUNIXPRINT... yes checking for
Re: how to compile pygtk in python2.7?
2.make the output is : Could not write method GdkFont.text_width_wc: No ArgType for const-GdkWChar* Could not write method GdkFont.char_width_wc: No ArgType for GdkWChar Could not write method GdkFont.text_extents_wc: No ArgType for const-GdkWChar* Could not write method GdkFont.string_extents: No ArgType for gint* Could not write method GdkFont.get_xfont: No ArgType for gpointer Could not write method GdkFont.x11_font_get_xdisplay: No ArgType for Display* Could not write method GdkCursor.get_xdisplay: No ArgType for Display* Could not write method GdkCursor.get_xcursor: No ArgType for Cursor Could not write method GdkRegion.spans_intersect_foreach: No ArgType for GdkSpan* Could not write method GdkRegion.rect_equal: No ArgType for const-GdkRectangle* Warning: generating old-style constructor for:gdk_colormap_new Could not write method GdkColormap.alloc_colors: No ArgType for gboolean* Could not write method GdkColormap.get_xdisplay: No ArgType for Display* Could not write method GdkColormap.get_xcolormap: No ArgType for Colormap Warning: generating old-style constructor for:gdk_display_open Could not write method GdkDisplay.add_client_message_filter: No ArgType for GdkFilterFunc Could not write method GdkDisplay.set_pointer_hooks: No ArgType for const-GdkDisplayPointerHooks* Could not write method GdkDisplay.get_xdisplay: No ArgType for Display* Could not write method GdkDisplay.set_cursor_theme: No ArgType for const-gint Could not write method GdkDrawable.set_data: No ArgType for gpointer Could not write method GdkDrawable.get_data: No ArgType for gpointer Could not write method GdkDrawable.draw_text_wc: No ArgType for const-GdkWChar* Could not write method GdkDrawable.get_xdisplay: No ArgType for Display* Could not write method GdkDrawable.get_xid: No ArgType for XID Could not write virtual accessor method GdkDrawable.create_gc: No ArgType for GdkGCValues* Could not write virtual accessor method GdkDrawable.draw_polygon: No ArgType for GdkPoint* Could not write virtual accessor method GdkDrawable.draw_text_wc: No ArgType for const-GdkWChar* Could not write virtual accessor method GdkDrawable.draw_points: No ArgType for GdkPoint* Could not write virtual accessor method GdkDrawable.draw_segments: No ArgType for GdkSegment* Could not write virtual accessor method GdkDrawable.draw_lines: No ArgType for GdkPoint* Could not write virtual accessor method GdkDrawable.get_size: No ArgType for gint* Could not write virtual accessor method GdkDrawable.get_composite_drawable: No ArgType for gint* Could not write virtual accessor method GdkDrawable.draw_trapezoids: No ArgType for GdkTrapezoid* Could not write virtual accessor method GdkDrawable.ref_cairo_surface: No ArgType for cairo_surface_t* Could not write virtual proxy GdkDrawable.create_gc: No ArgType for GdkGCValues* Could not write virtual proxy GdkDrawable.draw_polygon: No ArgType for GdkPoint* Could not write virtual proxy GdkDrawable.draw_text_wc: No ArgType for const-GdkWChar* Could not write virtual proxy GdkDrawable.draw_points: No ArgType for GdkPoint* Could not write virtual proxy GdkDrawable.draw_segments: No ArgType for GdkSegment* Could not write virtual proxy GdkDrawable.draw_lines: No ArgType for GdkPoint* Could not write virtual proxy GdkDrawable.get_size: cannot use int* parameter with direction 'None' Could not write virtual proxy GdkDrawable.get_composite_drawable: cannot use int* parameter with direction 'None' Could not write virtual proxy GdkDrawable.draw_trapezoids: No ArgType for GdkTrapezoid* Could not write virtual proxy GdkDrawable.ref_cairo_surface: No ArgType for cairo_surface_t* Could not write method GdkWindow.selection_property_get: No ArgType for guchar** Could not write method GdkWindow.remove_filter: No ArgType for GdkFilterFunc Could not write method GdkWindow.invalidate_maybe_recurse: No ArgType for gboolean (*child_func)(GdkWindow *, gpointer) Could not write method GdkWindow.get_internal_paint_info: No ArgType for GdkDrawable** Could not write method GdkWindow.get_root_coords: No ArgType for gint* Warning: generating old-style constructor for:gdk_pango_renderer_new Warning: generating old-style constructor for:gdk_pixmap_new Could not write method GdkGC.get_values: No ArgType for GdkGCValues* Could not write method GdkGC.get_xdisplay: No ArgType for Display* Could not write method GdkGC.get_xgc: No ArgType for GC Could not write virtual accessor method GdkGC.get_values: No ArgType for GdkGCValues* Could not write virtual accessor method GdkGC.set_values: No ArgType for GdkGCValues* Could not write virtual accessor method GdkGC.set_dashes: No ArgType for gint8[] Could not write virtual proxy GdkGC.get_values: No ArgType for GdkGCValues* Could not write virtual proxy GdkGC.set_values:
Re: Socket code not executing properly in a thread (Windows)
On 07/08/2012 11:03 AM, Andrew D'Angelo wrote: > Also, running the sned to socket code inside the main loop would not work, > as the main loop pauses until it recieves data from the socket (an IRC > message), which is why I didn't put the SMS code in the main loop in the > first place. http://docs.python.org/library/select.html -- http://mail.python.org/mailman/listinfo/python-list
How to safely maintain a status file
Hello, I want my script to generate a ~1KB status file several times a second. The script may be terminated at any time but the status file must not be corrupted. When the script is started next time the status file will be read to check what needs to be done. My initial solution was a thread that writes status to a tmp file first and then renames: open(tmp_file, 'w').write(status) os.rename(tmp_file, status_file) This works well on Linux but Windows raises an error when status_file already exists. http://docs.python.org/library/os.html#os.rename I guess I could delete the status file: open(tmp_file, 'w').write(status) if os.path.exists(status_file): os.remove(status_file) os.rename(tmp_file, status_file) and then on startup read from tmp_file if status_file does not exist. But this seems awkward. Is there a better way? Or do I need to use a database? Richard -- http://mail.python.org/mailman/listinfo/python-list
Re: How to safely maintain a status file
Am 08.07.2012 13:29, schrieb Richard Baron Penman: > My initial solution was a thread that writes status to a tmp file > first and then renames: > > open(tmp_file, 'w').write(status) > os.rename(tmp_file, status_file) You algorithm may not write and flush all data to disk. You need to do additional work. You must also store the tmpfile on the same partition (better: same directory) as the status file with open(tmp_file, "w") as f: f.write(status) # flush buffer and write data/metadata to disk f.flush() os.fsync(f.fileno()) # now rename the file os.rename(tmp_file, status_file) # finally flush metadata of directory to disk dirfd = os.open(os.path.dirname(status_file), os.O_RDONLY) try: os.fsync(dirfd) finally: os.close(dirfd) > This works well on Linux but Windows raises an error when status_file > already exists. > http://docs.python.org/library/os.html#os.rename Windows doesn't suppport atomic renames if the right side exists. I suggest that you implement two code paths: if os.name == "posix": rename = os.rename else: def rename(a, b): try: os.rename(a, b) except OSError, e: if e.errno != 183: raise os.unlink(b) os.rename(a, b) Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: How to safely maintain a status file
What are you keeping in this status file that needs to be saved several times per second? Depending on what type of state you're storing and how persistent it needs to be, there may be a better way to store it. Michael On Sun, Jul 8, 2012 at 7:53 AM, Christian Heimes wrote: > Am 08.07.2012 13:29, schrieb Richard Baron Penman: >> My initial solution was a thread that writes status to a tmp file >> first and then renames: >> >> open(tmp_file, 'w').write(status) >> os.rename(tmp_file, status_file) > > You algorithm may not write and flush all data to disk. You need to do > additional work. You must also store the tmpfile on the same partition > (better: same directory) as the status file > > with open(tmp_file, "w") as f: > f.write(status) > # flush buffer and write data/metadata to disk > f.flush() > os.fsync(f.fileno()) > > # now rename the file > os.rename(tmp_file, status_file) > > # finally flush metadata of directory to disk > dirfd = os.open(os.path.dirname(status_file), os.O_RDONLY) > try: > os.fsync(dirfd) > finally: > os.close(dirfd) > > >> This works well on Linux but Windows raises an error when status_file >> already exists. >> http://docs.python.org/library/os.html#os.rename > > Windows doesn't suppport atomic renames if the right side exists. I > suggest that you implement two code paths: > > if os.name == "posix": > rename = os.rename > else: > def rename(a, b): > try: > os.rename(a, b) > except OSError, e: > if e.errno != 183: > raise > os.unlink(b) > os.rename(a, b) > > Christian > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Discussion on some Code Issues
On Sunday, July 8, 2012 1:33:25 PM UTC+5:30, Chris Angelico wrote: > On Sun, Jul 8, 2012 at 3:42 PM, wrote: > > Thanks for pointing out the mistakes. Your points are right. So I am trying > > to revise it, > > > > file_open=open("/python32/doc1.txt","r") > > for line in file_open: > > line_word=line.split() > > print (line_word) > > Yep. I'd be inclined to rename file_open to something that says what > the file _is_, and you may want to look into the 'with' statement to > guarantee timely closure of the file, but that's a way to do it. > > Also, as has already been mentioned: keeping your data files in the > Python binaries directory isn't usually a good idea. More common to > keep them in the same directory as your script, which would mean that > you don't need a path on it at all. > > ChrisA Dear Chirs, No file path! Amazing. I do not know I like to know one small example please. Btw, some earlier post said, line.split() to convert line into bag of words can be done with power(), but I did not find it, if any one can help. I do close files do not worry. New style I'd try. Regards, Subha -- http://mail.python.org/mailman/listinfo/python-list
Re: Discussion on some Code Issues
On Mon, Jul 9, 2012 at 3:05 AM, wrote: > On Sunday, July 8, 2012 1:33:25 PM UTC+5:30, Chris Angelico wrote: >> On Sun, Jul 8, 2012 at 3:42 PM, wrote: >> > file_open=open("/python32/doc1.txt","r") >> Also, as has already been mentioned: keeping your data files in the >> Python binaries directory isn't usually a good idea. More common to >> keep them in the same directory as your script, which would mean that >> you don't need a path on it at all. > No file path! Amazing. I do not know I like to know one small example please. open("doc1.txt","r") Python will look for a file called doc1.txt in the directory you run the script from (which is often going to be the same directory as your .py program). > Btw, some earlier post said, line.split() to convert line into bag of words > can be done with power(), but I did not find it, if any one can help. I do > close files do not worry. New style I'd try. I don't know what power() function you're talking about, and can't find it in the previous posts; the nearest I can find is a post from Ranting Rick which says a lot of guff that you can ignore. (Rick is a professional troll. Occasionally he says something useful and courteous; more often it's one or the other, or neither.) As to the closing of files: There are a few narrow issues that make it worth using the 'with' statement, such as exceptions; mostly, it's just a good habit to get into. If you ignore it, your file will *usually* be closed fairly soon after you stop referencing it, but there's no guarantee. (Someone else will doubtless correct me if I'm wrong, but I'm pretty sure Python guarantees to properly flush and close on exit, but not necessarily before.) ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Discussion on some Code Issues
In article , Chris Angelico wrote: > open("doc1.txt","r") > > Python will look for a file called doc1.txt in the directory you run > the script from (which is often going to be the same directory as your > .py program). Well, to pick a nit, the file will be looked for in the current working directory. This may or may not be the directory you ran your script from. Your script could have executed chdir() between the time you started it and you tried to open the file. To pick another nit, it's misleading to say, "Python will look for...". This implies that Python somehow gets involved in pathname resolution, when it doesn't. Python just passes paths to the operating system as opaque strings, and the OS does all the magic of figuring out what that string means. -- http://mail.python.org/mailman/listinfo/python-list
Re: Discussion on some Code Issues
On 08/07/2012 18:17, Chris Angelico wrote: On Mon, Jul 9, 2012 at 3:05 AM, wrote: On Sunday, July 8, 2012 1:33:25 PM UTC+5:30, Chris Angelico wrote: On Sun, Jul 8, 2012 at 3:42 PM, wrote: > file_open=open("/python32/doc1.txt","r") Also, as has already been mentioned: keeping your data files in the Python binaries directory isn't usually a good idea. More common to keep them in the same directory as your script, which would mean that you don't need a path on it at all. No file path! Amazing. I do not know I like to know one small example please. open("doc1.txt","r") Python will look for a file called doc1.txt in the directory you run the script from (which is often going to be the same directory as your .py program). Btw, some earlier post said, line.split() to convert line into bag of words can be done with power(), but I did not find it, if any one can help. I do close >> files do not worry. New style I'd try. I don't know what power() function you're talking about, and can't find it in the previous posts; the nearest I can find is a post from Ranting Rick which says a lot of guff that you can ignore. (Rick is a professional troll. Occasionally he says something useful and courteous; more often it's one or the other, or neither.) I believe the relevant quote is """especially the Python gods have given you *power* over string objects""". If that's the case, he's not referring to a method or a function called "power". He did give the good warning about the problem there could be if the original string contains "$", the character being used as the separator. As to the closing of files: There are a few narrow issues that make it worth using the 'with' statement, such as exceptions; mostly, it's just a good habit to get into. If you ignore it, your file will *usually* be closed fairly soon after you stop referencing it, but there's no guarantee. (Someone else will doubtless correct me if I'm wrong, but I'm pretty sure Python guarantees to properly flush and close on exit, but not necessarily before.) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to safely maintain a status file
On Sun, 8 Jul 2012 21:29:41 +1000, Richard Baron Penman declaimed the following in gmane.comp.python.general: and then on startup read from tmp_file if status_file does not exist. But this seems awkward. It also violates your requirement -- since the "crash" could take place with a partial "temp file". I'd suggest that, rather than deleting the old status file, you rename IT -- and only delete it IF you successfully rename the temp file. Yes, this is much better. Almost perfect. Don't forget to consult your system documentation, and check if the rename operation is atomic or not. (Most probably it will only be atomic if the original and the renamed file are on the same physical partition and/or mount point). But even if the rename operation is atomic, there is still a race condition. Your program can be terminated after the original status file has been deleted, and before the temp file was renamed. In this case, you will be missing the status file (although your program already did something just it could not write out the new status). Here is an algorithm that can always write and read a status (but it might not be the latest one). You can keep the last two status files. Writer: *create temp file, write new status info * create lock file if needed * flock it * try: *delete older status file * rename temp file to new status file * finally: unlock the lock file Reader: * flock the lock file * try: *select the newer status file * read status info * finally: unlock the lock file It is guaranteed that you will always have a status to read, and in most cases this will be the last one (because the writer only locks for a short time). However, it is still questionable, because your writer may be waiting for the reader to unlock, so the new status info may not be written immediatelly. It would really help if you could tell us what are you trying to do that needs status. Best, Laszlo -- http://mail.python.org/mailman/listinfo/python-list
Tkinter.event.widget: handler gets name instead of widget.
Hi widget wizards, The manual describes the "event" attribute "widget" as "The widget which generated this event. This is a valid Tkinter widget instance, not a name. This attribute is set for all events." Ans so it is--has been until on the latest occasion "event.widget" was not the widget, but its name, crashing the handler. # Here I build a list of selectable records having each four fields. # The fields are labels. The selectable line is a frame containing the # labels side by side. The line frames go into self, which is a Frame. for n in range (len (records)): record = records [n] line_frame = Frame (self, name = '-%d-' % n, relief = RAISED, **BUTTON_FRAME_) line_frame.bind ('', self.enter) line_frame.bind ('', self.leave) line_frame.bind ('', self.pick_record) line_frame.bind ('', self.info_profile) line_frame.grid (row = n+2, column = 1) for i in self.range_n_fields: # (0, 1, 2, 3) field = Label (line_frame, text = record [self.INDICES [i]], anchor = W, width = self.COLUMN_WIDTHS [i], **DB_LIST_LABEL_) field.grid (row = 0, column = i, sticky = NW) # Here is the handler: def enter (self, event): w = event.widget print 'hit list.enter (). Event, widget', event, w, w.__class__ # Tracing line w.config (bg = SELECTED_BG_COLOR) # And here is what comes out. The first line is my tracing line. The name is correct in that it # names the entered line_frame, but is wrong because it should be the line_frame, not its name. # The rest is the red exception message: hit list.enter (). Event, widget .main-frame.data-frame.title-hit-list.-10- Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__ return self.func(*args) File "/home/fr/python/finance/piam/hit_list.py", line 83, in enter w.config (bg = SELECTABLE_BG_COLOR) AttributeError: 'str' object has no attribute 'config' # The same thing happens with . The other handlers I haven't done yet. The same bindings work well in # a Menu class with the difference that the bindings are on the Labels, not a containing Frame. # Dell E6500, Ubuntu 10.04, Python 2.6 Frederic -- http://mail.python.org/mailman/listinfo/python-list
Re: Confusing datetime.datetime
Because x1 and x2 have different time zones. The tzinfo field in x2 is equal to TZ and has a UTC offset of 1 hour. The tzinfo field in x1 contains the DST version of that timezone, with a UTC offset of 2 hours, because Skopje is currently on DST. I think you want: x2 = TZ.localize(datetime(x1.year, x1.month, x1.day)) That produces a datetime with the year, month and day set as indicated and tzinfo set to the correct UTC offset for that date, at 00:00 hours. Or maybe you need: x2 = TZ.localize(datetime(x1.year, x1.month, x1.day, 12)) x2 = x2.replace(hour=0) That determines whether DST should be on at noon, and then resets the hour field to zero. This produces the same outcome as the one liner, except on days when DST is switched on or off. Thanks, I think this will help me. Although these issues seem very much underdocumented in the datetime documentation. Are there any good references of using good times in Python? -- damjan -- http://mail.python.org/mailman/listinfo/python-list
Re: How to safely maintain a status file
Am 08.07.2012 22:57, schrieb Laszlo Nagy: > But even if the rename operation is atomic, there is still a race > condition. Your program can be terminated after the original status file > has been deleted, and before the temp file was renamed. In this case, > you will be missing the status file (although your program already did > something just it could not write out the new status). You are contradicting yourself. Either the OS is providing a fully atomic rename or it doesn't. All POSIX compatible OS provide an atomic rename functionality that renames the file atomically or fails without loosing the target side. On POSIX OS it doesn't matter if the target exists. You don't need locks or any other fancy stuff. You just need to make sure that you flush the data and metadata correctly to the disk and force a re-write of the directory inode, too. It's a standard pattern on POSIX platforms and well documented in e.g. the maildir RFC. You can use the same pattern on Windows but it doesn't work as good and doesn't guaranteed file integrity for two reasons: 1) Windows's rename isn't atomic if the right side exists. 2) Windows locks file when a program opens a file. Other programs can't rename or overwrite the file. (You can get around the issue with some extra work, though.) Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Discussion on some Code Issues
On Mon, Jul 9, 2012 at 4:17 AM, Roy Smith wrote: > In article , > Chris Angelico wrote: > >> open("doc1.txt","r") >> >> Python will look for a file called doc1.txt in the directory you run >> the script from (which is often going to be the same directory as your >> .py program). > > Well, to pick a nit, the file will be looked for in the current working > directory. This may or may not be the directory you ran your script > from. Your script could have executed chdir() between the time you > started it and you tried to open the file. > > To pick another nit, it's misleading to say, "Python will look for...". > This implies that Python somehow gets involved in pathname resolution, > when it doesn't. Python just passes paths to the operating system as > opaque strings, and the OS does all the magic of figuring out what that > string means. Two perfectly accurate nitpicks. And of course, there's a million and one other things that could happen in between, too, including possibilities of the current directory not even existing and so on. I merely oversimplified in the hopes of giving a one-paragraph explanation of what it means to not put a path name in your open() call :) It's like the difference between reminder text on a Magic: The Gathering card and the actual entries in the Comprehensive Rules. Perfect example is the "Madness" ability - the reminder text explains the ability, but uses language that actually is quite incorrect. It's a better explanation, though. Am I overanalyzing this? Yeah, probably... ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Discussion on some Code Issues
On Mon, 09 Jul 2012 07:54:47 +1000, Chris Angelico wrote: > It's like > the difference between reminder text on a Magic: The Gathering card and > the actual entries in the Comprehensive Rules. Perfect example is the > "Madness" ability - the reminder text explains the ability, but uses > language that actually is quite incorrect. It's a better explanation, > though. Hang on, you say that an explanation which is "quite incorrect" is *better* than one which is correct? I can see why they call the card "Madness". :-P -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: stuck in files!!
On Jul 6, 9:53 pm, Chirag B wrote: > i want to kno how to link two applications using python for eg:notepad > txt file and some docx file. like i wat to kno how to take path of > those to files and run them simultaneously.like if i type something in > notepad it has to come in wordpad whenever i run that code. Here's an approach that opens Notepad & Word, then monitors Notepad for any changes, updating the open Word document when they occur. This hasn't been tested in any great depth, and I wouldn't run it anywhere with existing instances of Notepad or Word open without testing what happens first, but it should get you started. For the most part this uses standard library files, although I did go with WinGuiAuto as a quick and dirty wrapper around the win32gui complexity. If you don't want to introduce it as a dependency, you can extract the functionality you need: http://www.brunningonline.net/simon/blog/archives/winGuiAuto.py.html import os import time from win32com.client import Dispatch from winguiauto import ( findTopWindow, findControl, getEditText, WinGuiAutoError ) # conditional for main loop def window_is_open(hwnd): try: findTopWindow(selectionFunction=lambda ohwnd: hwnd == ohwnd) return True except WinGuiAutoError: return False # start Notepad os.system('start notepad') time.sleep(0.1) # give it time to open or we don't get the handleW npad = findTopWindow(wantedText='Untitled - Notepad') ndoc = findControl(npad, wantedClass='Edit') # edit control for Notepad # start Word word = Dispatch("Word.Application") word.Visible = True wdoc = word.Documents.Add() # edit control (new document) for Word # update Word when Notepad changes MEMORY = None while window_is_open(npad): ntxt = getEditText(ndoc) if ntxt != MEMORY: wdoc.Content.Text = os.linesep.join(ntxt) MEMORY = ntxt time.sleep(1) wdoc.Close() # opens a save dialogue wdoc.Quit() Hope this helps. -- http://mail.python.org/mailman/listinfo/python-list
how can I make it work?
it's a finder using threading to accelerate but it never works.. and I have no idea why it doesn't work:( it doesn't work too after changing threading to multiprocessing.. how can I make it work? or at least I know what's the problem... plz help the poor newbie... import os,threading,multiprocessing def finder(path,q): for x in os.walk(unicode(path)): if x[1]: for dirname in x[1]: if target in dirname.lower(): q.put(os.path.join(x[0],dirname)) if x[2]: for name in x[2]: if target in name.lower(): q.put(os.path.join(x[0],name)) q.put(1) def printer(q): cmd=0 while 1: tmp=q.get() if tmp==1: cmd += 1 continue if cmd ==thnum: break print tmp if __name__ =="__main__": q=multiprocessing.JoinableQueue() ini=os.walk(u"C:\\").next() thnum=len(ini[1]) target=raw_input("what you wanna get\n") p=multiprocessing.Process(target=printer,args=(q,)) p.daemon=1 p.start() for i in xrange(thnum): t=threading.Thread(target=finder,args=(ini[1][i],q,)) t.start() print i," started" q.join() -- http://mail.python.org/mailman/listinfo/python-list
Re: how can I make it work?
On 09/07/2012 03:02, self.python wrote: it's a finder using threading to accelerate but it never works.. and I have no idea why it doesn't work:( it doesn't work too after changing threading to multiprocessing.. how can I make it work? or at least I know what's the problem... plz help the poor newbie... [snip] It would help if you gave more information than just "it doesn't work". In what way doesn't it work? -- http://mail.python.org/mailman/listinfo/python-list
Re: something go wrongly
If you do want an in-place extension, you could try: aList=[1,2,3,4,5,6,7,8,9,10] xList=[1,2,3] print "The concatenated lists are:", aList + bList Though you need to remember that neither aList nor bList is altered in this situation! Matthew Lefavor NASA GSFC [Microtel, LLC] Mail Code 699.0/Org Code 582.0 matthew.lefa...@nasa.gov (301) 614-6818 (Desk) (443) 758-4891 (Cell) From: levi nie To: "python-list@python.org" Subject: Re: something go wrongly Thanks, Such methods return None to emphasize that they do not create new lists. i got it. 2012/7/8 Chris Rebert On Sat, Jul 7, 2012 at 10:23 AM, levi nie wrote: > my code: > > aList=[1,2,3,4,5,6,7,8,9,10] > xList=[1,2,3] > print "now aList is",aList.extend(xList) > > output: > now aList is None > > what i want is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3] See http://stackoverflow.com/a/1682601 list.extend(), list.append(), etc. operate in-place, mutating the existing list object. Such methods return None to emphasize that they do not create new lists. Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: how can I make it work?
2012년 7월 9일 월요일 오전 11시 2분 41초 UTC+9, self.python 님의 말: > it's a finder using threading to accelerate but it never works.. > and I have no idea why it doesn't work:( > it doesn't work too after changing threading to multiprocessing.. > how can I make it work? or at least I know what's the problem... > plz help the poor newbie... > > > > import os,threading,multiprocessing > > > def finder(path,q): > for x in os.walk(unicode(path)): > if x[1]: >for dirname in x[1]: > if target in dirname.lower(): > q.put(os.path.join(x[0],dirname)) > if x[2]: >for name in x[2]: > if target in name.lower(): > q.put(os.path.join(x[0],name)) > > q.put(1) > > def printer(q): > cmd=0 > while 1: > tmp=q.get() > if tmp==1: >cmd += 1 >continue > if cmd ==thnum: >break > print tmp > > if __name__ =="__main__": > q=multiprocessing.JoinableQueue() > ini=os.walk(u"C:\\").next() > thnum=len(ini[1]) > target=raw_input("what you wanna get\n") > > p=multiprocessing.Process(target=printer,args=(q,)) > p.daemon=1 > p.start() > > for i in xrange(thnum): > t=threading.Thread(target=finder,args=(ini[1][i],q,)) > t.start() > print i," started" > q.join() It shows 0 started 1 started ... 22 started but then it show nothing the cursur blinks continuously but the result never printed(maybe not produces?) -- http://mail.python.org/mailman/listinfo/python-list
Re: how can I make it work?
2012년 7월 9일 월요일 오전 11시 41분 32초 UTC+9, self.python 님의 말: > 2012년 7월 9일 월요일 오전 11시 2분 41초 UTC+9, self.python 님의 말: > > it's a finder using threading to accelerate but it never works.. > > and I have no idea why it doesn't work:( > > it doesn't work too after changing threading to multiprocessing.. > > how can I make it work? or at least I know what's the problem... > > plz help the poor newbie... > > > > > > > > import os,threading,multiprocessing > > > > > > def finder(path,q): > > for x in os.walk(unicode(path)): > > if x[1]: > >for dirname in x[1]: > > if target in dirname.lower(): > > q.put(os.path.join(x[0],dirname)) > > if x[2]: > >for name in x[2]: > > if target in name.lower(): > > q.put(os.path.join(x[0],name)) > > > > q.put(1) > > > > def printer(q): > > cmd=0 > > while 1: > > tmp=q.get() > > if tmp==1: > >cmd += 1 > >continue > > if cmd ==thnum: > >break > > print tmp > > > > if __name__ =="__main__": > > q=multiprocessing.JoinableQueue() > > ini=os.walk(u"C:\\").next() > > thnum=len(ini[1]) > > target=raw_input("what you wanna get\n") > > > > p=multiprocessing.Process(target=printer,args=(q,)) > > p.daemon=1 > > p.start() > > > > for i in xrange(thnum): > > t=threading.Thread(target=finder,args=(ini[1][i],q,)) > > t.start() > > print i," started" > > q.join() > > > It shows > 0 started > 1 started > ... > 22 started > but then it show nothing > the cursur blinks continuously > but the result never printed(maybe not produces?) I knew what's the problem on other site. It just because I miss to send the full path to each thread... what an idiot.. but I'm still wondering that there is any good way -- http://mail.python.org/mailman/listinfo/python-list
Re: How to safely maintain a status file
> What are you keeping in this status file that needs to be saved > several times per second? Depending on what type of state you're > storing and how persistent it needs to be, there may be a better way > to store it. > > Michael This is for a threaded web crawler. I want to cache what URL's are currently in the queue so if terminated the crawler can continue next time from the same point. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter.event.widget: handler gets name instead of widget.
On 7/8/2012 5:19 PM, Frederic Rentsch wrote: Hi widget wizards, The manual describes the "event" attribute "widget" as "The widget which generated this event. This is a valid Tkinter widget instance, not a name. This attribute is set for all events." Same in 3.3, with nice example of using it. def turnRed(self, event): event.widget["activeforeground"] = "red" self.button.bind("", self.turnRed) Ans so it is--has been until on the latest occasion "event.widget" was not the widget, but its name, crashing the handler. Has event.widget been the widget only in other programs or previously with the same program? # Here I build a list of selectable records having each four fields. # The fields are labels. The selectable line is a frame containing the # labels side by side. The line frames go into self, which is a Frame. for n in range (len (records)): for n, record in enumerate(records): record = records [n] line_frame = Frame (self, name = '-%d-' % n, relief = RAISED, **BUTTON_FRAME_) line_frame.bind ('', self.enter) line_frame.bind ('', self.leave) line_frame.bind ('', self.pick_record) line_frame.bind ('', self.info_profile) line_frame.grid (row = n+2, column = 1) for i in self.range_n_fields: # (0, 1, 2, 3) field = Label (line_frame, text = record [self.INDICES [i]], anchor = W, width = self.COLUMN_WIDTHS [i], **DB_LIST_LABEL_) field.grid (row = 0, column = i, sticky = NW) When posting problem code, you should post a minimal, self-contained example that people can try on other systems and versions. Can you create the problem with one record, which you could give, and one binding? Do you need 4 fields, or would 1 'work'? # Here is the handler: def enter (self, event): w = event.widget print 'hit list.enter (). Event, widget', event, w, w.__class__ # Tracing line w.config (bg = SELECTED_BG_COLOR) # And here is what comes out. The first line is my tracing line. The name is correct in that it # names the entered line_frame, but is wrong because it should be the line_frame, not its name. # The rest is the red exception message: hit list.enter (). Event, widget .main-frame.data-frame.title-hit-list.-10- Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__ return self.func(*args) File "/home/fr/python/finance/piam/hit_list.py", line 83, in enter w.config (bg = SELECTABLE_BG_COLOR) AttributeError: 'str' object has no attribute 'config' # The same thing happens with . The other handlers I haven't done yet. The same bindings work well in # a Menu class with the difference that the bindings are on the Labels, not a containing Frame. # Dell E6500, Ubuntu 10.04, Python 2.6 You might try a current Python release, and the latest tcl/tk 8.5.11 released last March (comes with 3.3.0 Windows release, don't know how on Ubuntu). There might be (have been?) a bug with events on Frames, or on Frames within Frames treated as widgets. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: How to safely maintain a status file
> > and then on startup read from tmp_file if status_file does not exist. > > But this seems awkward. > > It also violates your requirement -- since the "crash" could take > place with a partial "temp file". Can you explain why? My thinking was if crash took place when writing the temp file this would not matter because the status file would still exist and be read from. The temp file would only be renamed when fully written. -- http://mail.python.org/mailman/listinfo/python-list
Re: how can I make it work?
On 7/8/2012 10:02 PM, self.python wrote: it's a finder using threading to accelerate Threading with Python does not accelerate unless all but one of the treads are i/o bound. You need multiple processes to use multiple cores in parallel. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: How to safely maintain a status file
> Windows doesn't suppport atomic renames if the right side exists. I > suggest that you implement two code paths: > > if os.name == "posix": > rename = os.rename > else: > def rename(a, b): > try: > os.rename(a, b) > except OSError, e: > if e.errno != 183: > raise > os.unlink(b) > os.rename(a, b) Problem is if the process is stopped between unlink and rename there would no status file. -- http://mail.python.org/mailman/listinfo/python-list