Re: how to embed non-tkinter VLC player into grid of tkinter with python?
after several trial, still can not put the player into grid import vlc from Tkinter import * import os import sys import ttk import Tkinter as tk class SampleApp(tk.Frame): def __init__(self, parent, title=None): #def __init__(self, *args, **kwargs): #tk.Tk.__init__(self, *args, **kwargs) tk.Frame.__init__(self, parent) self.parent = parent self.parent.title("video") self.player = None self.Instance = vlc.Instance('--no-audio') self.player = self.Instance.media_player_new() #self.player.set_xwindow(ttk.Frame(self.parent).winfo_id()) p=self.Instance.media_player_new() m=self.Instance.media_new('file:///home/martin/Downloads/autoweb.mp4') p.set_media(m) p.play() #self.videopanel = #p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4') #p.play() self.register(self, parent) #self.player.grid(row=1,column=1) #def GetHandle(self): #return self.videopanel.winfo_id() root = Tk() for r in range(2): for c in range(1): termf = Frame(root, height=200, width=300) termf.pack(fill=BOTH, expand=YES) wid = termf.winfo_id() #player = Player() SampleApp(root, title="tkinter vlc").grid(row=r,column=c) #os.system('xterm -into %d -geometry 40x20 -sb &' % wid) #os.system('vlc --no-fullscreen "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' % wid) root.mainloop() On Thursday, March 16, 2017 at 1:16:41 PM UTC+8, Ho Yeung Lee wrote: > martin@ubuntu:~/Downloads/python-vlc/examples$ xdg-screensaver: Window > 0x0900 does not exist > > error when run after inherit a frame which put in a grid > > import vlc > from Tkinter import * > import os > > import Tkinter as tk > > class SampleApp(tk.Frame): > def __init__(self, parent, title=None): > #def __init__(self, *args, **kwargs): > #tk.Tk.__init__(self, *args, **kwargs) > tk.Frame.__init__(self, parent) > self.parent = parent > self.parent.title("video") > p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4') > p.play() > self.register() > #self.grid(row=1,column=1) > > root = Tk() > for r in range(5): > for c in range(5): > termf = Frame(root, height=200, width=300) > termf.pack(fill=BOTH, expand=YES) > wid = termf.winfo_id() > #player = Player() > SampleApp(root, title="tkinter vlc").grid(row=r,column=c) > #os.system('xterm -into %d -geometry 40x20 -sb &' % wid) > #os.system('vlc --no-fullscreen > "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' > % wid) > > root.mainloop() > > > On Thursday, March 16, 2017 at 12:40:16 PM UTC+8, Ho Yeung Lee wrote: > > we have many TV that would like to be monitored, > > > > how to embed non-tkinter VLC player into grid of tkinter with python? > > > > below code can embeded xterm but not for VLC player > > > > > > import vlc > > from Tkinter import * > > import os > > > > root = Tk() > > for r in range(2): > > for c in range(1): > > termf = Frame(root, height=100, width=200) > > termf.pack(fill=BOTH, expand=YES) > > wid = termf.winfo_id() > > termf.grid(row=r,column=c) > > p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4') > > p.get_tk_widget().grid(row=r,column=c) > > p.play() > > #os.system('xterm -into %d -geometry 40x20 -sb &' % wid) > > os.system('vlc --no-fullscreen > > "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb > > &' % wid) > > > > root.mainloop() -- https://mail.python.org/mailman/listinfo/python-list
Re: how to embed non-tkinter VLC player into grid of tkinter with python?
martin@ubuntu:~/Downloads/python-vlc/examples$ xdg-screensaver: Window 0x0900 does not exist error when run after inherit a frame which put in a grid import vlc from Tkinter import * import os import Tkinter as tk class SampleApp(tk.Frame): def __init__(self, parent, title=None): #def __init__(self, *args, **kwargs): #tk.Tk.__init__(self, *args, **kwargs) tk.Frame.__init__(self, parent) self.parent = parent self.parent.title("video") p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4') p.play() self.register() #self.grid(row=1,column=1) root = Tk() for r in range(5): for c in range(5): termf = Frame(root, height=200, width=300) termf.pack(fill=BOTH, expand=YES) wid = termf.winfo_id() #player = Player() SampleApp(root, title="tkinter vlc").grid(row=r,column=c) #os.system('xterm -into %d -geometry 40x20 -sb &' % wid) #os.system('vlc --no-fullscreen "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' % wid) root.mainloop() On Thursday, March 16, 2017 at 12:40:16 PM UTC+8, Ho Yeung Lee wrote: > we have many TV that would like to be monitored, > > how to embed non-tkinter VLC player into grid of tkinter with python? > > below code can embeded xterm but not for VLC player > > > import vlc > from Tkinter import * > import os > > root = Tk() > for r in range(2): > for c in range(1): > termf = Frame(root, height=100, width=200) > termf.pack(fill=BOTH, expand=YES) > wid = termf.winfo_id() > termf.grid(row=r,column=c) > p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4') > p.get_tk_widget().grid(row=r,column=c) > p.play() > #os.system('xterm -into %d -geometry 40x20 -sb &' % wid) > os.system('vlc --no-fullscreen > "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' > % wid) > > root.mainloop() -- https://mail.python.org/mailman/listinfo/python-list
how to embed non-tkinter VLC player into grid of tkinter with python?
we have many TV that would like to be monitored, how to embed non-tkinter VLC player into grid of tkinter with python? below code can embeded xterm but not for VLC player import vlc from Tkinter import * import os root = Tk() for r in range(2): for c in range(1): termf = Frame(root, height=100, width=200) termf.pack(fill=BOTH, expand=YES) wid = termf.winfo_id() termf.grid(row=r,column=c) p=vlc.MediaPlayer('file:///home/martin/Downloads/autoweb.mp4') p.get_tk_widget().grid(row=r,column=c) p.play() #os.system('xterm -into %d -geometry 40x20 -sb &' % wid) os.system('vlc --no-fullscreen "file:///home/martin/Downloads/autoweb.mp4" -into %d -geometry 100x200 -sb &' % wid) root.mainloop() -- https://mail.python.org/mailman/listinfo/python-list
RE: Dynamically replacing an objects __class__; is it safe?
MRAB wrote, on Wednesday, March 15, 2017 3:19 PM > > On 2017-03-15 22:03, Gregory Ewing wrote: > > Steve D'Aprano wrote: > >> You probably can't make a whale fly just by changing the class to > >> bird. It will need wings, and feathers, at the very least. > > > > Some things succeed in flying with neither wings nor feathers. > > Helicopters, for example. > > > Could you argue that the blades were a kind of wing? After all, they > rely on the same principle of moving through the air to produce lift. > > Balloons, on the other hand, ... :-) or kites... ;) -- https://mail.python.org/mailman/listinfo/python-list
Re: crc method for a serial driver for fiscal cash register
On 2017-03-16 00:08, Chris Angelico wrote: On Thu, Mar 16, 2017 at 9:31 AM, MRAB wrote: 3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted. Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including byte END. 4. TR is loaded with 0 and point 3 is executed 5. TR is loaded with 0 and point 3 is executed 6. Byte S1 is transmitted 7. Byte S0 is transmitted [snip] Step 3 is a little unclear. Step 3.1 says to shift TR, but step 3.2 says to check the carry bit after shifting S1, which is zero initially, therefore the carry bit will be clear, therefore S1 and S0 won't have any bits inverted and will stay zero. I think probably "if the carry bit from TR is 1" would make this all make sense. Otherwise TR, which is the data payload, is completely ignored. Hence the need for examples. -- https://mail.python.org/mailman/listinfo/python-list
SimpleHTTPServer and CgiHTTPServer in practice
Sometimes I have a short term requirement to serve some data by http, so I've been using those modules rather than setting up a full featured web server. Some Python users have told me that isn't a good idea, but without any specifics. Are there any known problems with them such as security bugs, or in the py3 equivalents (server module)? I haven't had issues so far because it's just been ad hoc stuff for known clients, but they are much simpler to set up than something like Apache, so I don't see why not to make more use of them for low-traffic pages. Any thoughts/stories/cautions would be appreciated. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: crc method for a serial driver for fiscal cash register
On Thu, Mar 16, 2017 at 9:31 AM, MRAB wrote: >> 3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are >> inverted. >> Points 2 and 3 are executed for all bytes, included in the calculation of >> the CRC - from the first byte after BEG up to and including byte END. >> 4. TR is loaded with 0 and point 3 is executed >> 5. TR is loaded with 0 and point 3 is executed >> 6. Byte S1 is transmitted >> 7. Byte S0 is transmitted >> > [snip] > Step 3 is a little unclear. > > Step 3.1 says to shift TR, but step 3.2 says to check the carry bit after > shifting S1, which is zero initially, therefore the carry bit will be clear, > therefore S1 and S0 won't have any bits inverted and will stay zero. > I think probably "if the carry bit from TR is 1" would make this all make sense. Otherwise TR, which is the data payload, is completely ignored. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: crc method for a serial driver for fiscal cash register
On 2017-03-15 18:18, Bogdan Radu Bolchis wrote: hi, i'm developing a Point Of Sale app in python for my company, and we need to integrate the fiscal cash register driver in python. The original driver is only for windows and no longer supported, we want to switch to Linux. We managed to find the serial protocol, but the sent data is followed by a CRC. Can someone help me building a CRC function according to the manufacturer data? Here is what i have: ALGORITHM FOR CRC CALCULATION The two CRC bytes are calculated according to the formula x^15 + 1. In the calculation are included all data bytes plus the byte for block end. Every byte passes through the calculation register from teh MSB to LSB. Three working bytes are used - S1, S0 and TR S1 - Most significant byte from the CRC ( it is transmitted immediatelly after END) S0 - Least significant byte from the CRC ( It is transmitted after S1) TR - the current transmitted byte in the block. The CRC is calculated as follows: 1. S1 and S0 are zeroed 2. TR is loaded with the current transmitted byte. The byte is transmitted. 3. Points 3.1 and 3.2 are executed 8 times: 3.1. S1, S0 and TR are shifted one bit to the left. 3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted. Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including byte END. 4. TR is loaded with 0 and point 3 is executed 5. TR is loaded with 0 and point 3 is executed 6. Byte S1 is transmitted 7. Byte S0 is transmitted [snip] Step 3 is a little unclear. Step 3.1 says to shift TR, but step 3.2 says to check the carry bit after shifting S1, which is zero initially, therefore the carry bit will be clear, therefore S1 and S0 won't have any bits inverted and will stay zero. In summary, the CRC won't be affected by the data _at all_. And how about a few examples for checking the algorithm? -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thu, Mar 16, 2017 at 9:19 AM, MRAB wrote: > On 2017-03-15 22:03, Gregory Ewing wrote: >> >> Steve D'Aprano wrote: >>> >>> You probably can't make a whale fly just by changing the class to bird. >>> It >>> will need wings, and feathers, at the very least. >> >> >> Some things succeed in flying with neither wings nor feathers. >> Helicopters, for example. >> > Could you argue that the blades were a kind of wing? After all, they rely on > the same principle of moving through the air to produce lift. > > Balloons, on the other hand, ... :-) Certainly not. If they were, you'd move (or spin) them faster to go up, and slower to go down. But you don't. So it's all about the ugliness. You twist the blades up a bit to make the helicopter uglier. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On 2017-03-15 22:03, Gregory Ewing wrote: Steve D'Aprano wrote: You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. Some things succeed in flying with neither wings nor feathers. Helicopters, for example. Could you argue that the blades were a kind of wing? After all, they rely on the same principle of moving through the air to produce lift. Balloons, on the other hand, ... :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Thu, Mar 16, 2017 at 9:03 AM, Gregory Ewing wrote: > Steve D'Aprano wrote: >> >> You probably can't make a whale fly just by changing the class to bird. It >> will need wings, and feathers, at the very least. > > > Some things succeed in flying with neither wings nor feathers. > Helicopters, for example. Yeah but they do it by being so ugly that the earth repels them. I'm not sure that whale.ugliness is valid or high enough. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
Steve D'Aprano wrote: You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. Some things succeed in flying with neither wings nor feathers. Helicopters, for example. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
crc method for a serial driver for fiscal cash register
hi, i'm developing a Point Of Sale app in python for my company, and we need to integrate the fiscal cash register driver in python. The original driver is only for windows and no longer supported, we want to switch to Linux. We managed to find the serial protocol, but the sent data is followed by a CRC. Can someone help me building a CRC function according to the manufacturer data? Here is what i have: ALGORITHM FOR CRC CALCULATION The two CRC bytes are calculated according to the formula x^15 + 1. In the calculation are included all data bytes plus the byte for block end. Every byte passes through the calculation register from teh MSB to LSB. Three working bytes are used - S1, S0 and TR S1 - Most significant byte from the CRC ( it is transmitted immediatelly after END) S0 - Least significant byte from the CRC ( It is transmitted after S1) TR - the current transmitted byte in the block. The CRC is calculated as follows: 1. S1 and S0 are zeroed 2. TR is loaded with the current transmitted byte. The byte is transmitted. 3. Points 3.1 and 3.2 are executed 8 times: 3.1. S1, S0 and TR are shifted one bit to the left. 3.2. If the carry bit from S1 is 1, the MSB of S1 and LSB of S0 are inverted. Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including byte END. 4. TR is loaded with 0 and point 3 is executed 5. TR is loaded with 0 and point 3 is executed 6. Byte S1 is transmitted 7. Byte S0 is transmitted ALGORITHM FOR CRC CHECK ON RECEIVING Three working bytes are used S1, S0 and RC S1 - Most significant byte from the CRC ( it is received immediately after END) S0 - Least significant byte from the CRC ( transmitted after S1) RC - the current received byte in the block ( beginning from the first byte after BEG and ending 2 bytes after END). The CRC is obtained as follows: 1. S1 and S0 are zeroed 2. RC is loaded with the current received byte 3. Points 3.1 and 3.2 are executed 8 times: 3.1. S1, S0 and RC are shifted 8 times to the left 3.2. if the MSB of S1 is 1 then MSB of S1 and LSB of S0 are inverted. Points 2 and 3 are executed for all bytes, included in the calculation of the CRC - from the first byte after BEG up to and including 2 bytes after END. S1 and S0 must be 0. thank you -- https://mail.python.org/mailman/listinfo/python-list
Re: python and databases
On 03/15/2017 08:45 AM, Xristos Xristoou wrote: > first thx for response second i cant use additional packate because i > build tool for other python program and that use standar python > only, I don't see how you can retrieve data from MS Access without a tool that is not part of the standard Python distribution. > sqlite is in standar python 2.7 @Irmen de Jong ? Yes. Sqlite3 is built into Python 2.7. However I don't see how it will help you with an access database. You'd have to convert the data from MS Access to sql and insert it into the sqlite database. > can use quyries in CSV @ Irving Duran @Chris Angelico ? No. CSV is just a data interchange format. It's not a database. However you could export the entire table from MS Access to CSV and then with a Python script create an SQLite table and insert records into it from the CSV file that you load and parse with Python. -- https://mail.python.org/mailman/listinfo/python-list
Re: python and databases
Τη Τρίτη, 14 Μαρτίου 2017 - 9:59:42 μ.μ. UTC+2, ο χρήστης Xristos Xristoou έγραψε: first thx for response second i cant use additional packate because i build tool for other python program and that use standar python only, sqlite is in standar python 2.7 @Irmen de Jong ? can use quyries in CSV @ Irving Duran @Chris Angelico ? -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract items from string in db
On 03/14/2017 03:26 PM, DFS wrote: > I have a SQLite database with TEXT columns containing strings of the format: > > ['Item1: Value1 has,comma','Item2: has'apostrophe','Item3: Value3'] > > The brackets are part of the string. > > How can I unpack and access each item? > Item1: Value1 has,comma > Item2: has'apostrophe > Item3: Value3 > This problem is probably unsolvable for all cases because the data is apparently allowed to freely contain the characters that provide the string with its structure. Still, there are clues that enable the parsing of most normal cases you are likely to encounter. This hinges on the fact that "','" is unlikely to occur in the actual data: s = "['Item1: Value1 has,comma','Item2: has'apostrophe','Item3: Value3']" items = s[2:-2].split("','") # Toss brackets, first and last quotes for thing in items: key, val = thing.split(": ") print "%s: %s" % (key, val) Output: --- Item1: Value1 has,comma Item2: has'apostrophe Item3: Value3 -- https://mail.python.org/mailman/listinfo/python-list
numpy indexing performance
This is a simplified example of a Monte Carlo simulation where random vectors (here 2D vectors, which are all zero) are summed (the result is in r1 and r2 or r, respectively): def case1(): import numpy as np M = 10 N = 1 r1 = np.zeros(M) r2 = np.zeros(M) s1 = np.zeros(N) s2 = np.zeros(N) for n in range(1000): ind = np.random.random_integers(N, size=M) - 1 r1 += s1[ind] r2 += s2[ind] def case2(): import numpy as np M = 10 N = 1 r = np.zeros((M, 2)) s = np.zeros((N, 2)) for n in range(1000): ind = np.random.random_integers(N, size=M) - 1 r += s[ind] import timeit print("case1:", timeit.timeit( "case1()", setup="from __main__ import case1", number=1)) print("case2:", timeit.timeit( "case2()", setup="from __main__ import case2", number=1)) Resulting in: case1: 2.6224704339983873 case2: 4.374910838028882 Why is case2 significantly slower (almost by a factor of 2) than case1? There should be the same number of operations (additions) in both cases; the main difference is the indexing. Is there another (faster) way to avoid the separate component arrays r1 and r2? (I also tried r = np.zeros(M, dtype='2d'), which was comparable to case2.) Olaf -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
Hi, You can subclass M2 from M1 and override only what you need. Regards, -- https://mail.python.org/mailman/listinfo/python-list
Re: Dynamically replacing an objects __class__; is it safe?
On Wed, 15 Mar 2017 08:54 pm, 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. *Generally* safe? No. You cannot expect to take an arbitrary object and change its class: animal = Whale() animal.__class__ = Bird animal.fly() You probably can't make a whale fly just by changing the class to bird. It will need wings, and feathers, at the very least. For pure-Python classes, it is "safe" in the sense that the worst that will happen is an exception, not a seg fault or core dump. But it is not safe in the sense that the instance will work correctly: methods may fail, or do he wrong thing. (Actually, *silently doing the wrong thing* is usually much worse than raising an exception or crashing. At least when the program crashes, you know it is broken!) For *cooperative* classes, where you design the Before and After classes to operate correctly when you change the __class__, that's perfectly 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. Sounds scarily confusing, and I say that as somebody who LOVES the "swap the __class__" trick when it is appropriate. If I wouldn't risk swapping out the __class__ if I didn't control the classes. You have this: o = Base() o.my_prop # Prints 'Initializing M1' o = Base() o.__class__ = ShadowBase o.my_prop # Prints 'Initializing M2' But why not just do this? o = ShadowBase() # inherits from Base, overrides my_prop o.my_prop # Prints 'Initializing M2' This sounds like an ordinary example of class inheritance to me, no need to add complications with swapping out the __class__ for something different. The only reason I would consider using the __class__ trick here is if you don't control the instantiation of the object: you don't create your own instance, you receive it pre-instantiated from elsewhere. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list
How to make selected row visible in GTK TreeView?
I have a GTK TreeView with scrollbars which enables me to scroll through the values OK. Clicking on a row selects it and highlights it. However if one uses 'down arrow' to move the selection/highlight down the list then the selected row disappears from the visibble window. How can I scroll the window so that the selected row remains visible? I would also like a 'go to row x' function to scroll to a specific row in the list and highlight it. I guess this would need very similar code to the first requirement. -- Chris Green · -- 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
Re: Dynamically replacing an objects __class__; is it safe?
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 > (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
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: python and databases
Am 15.03.2017 um 01:23 schrieb Michael Torrie: On 03/14/2017 01:59 PM, Xristos Xristoou wrote: I have a database in microsoft ACCESS with about 150 records.. if I want to get some data from this database using a query in python and i want to store in some variables in python that will do this ? to avoid the 150 if ...: Using the standard library the python? know easily put it an SQL query but i canot use additional library python. If you simply want to move data to an sql server, your best bet is to use mdbtools to dump the database schema and records out to an .sql file that you can then massage and import into whatever sql server you are using. https://github.com/brianb/mdbtools If you're looking for a way to use the access file directly, and continue using it in MS Access, you might be able to get the odbc bridge working (on windows of course), and communicate with it with pyodbc. Or you could use adodbapi. Depending on your Python installation you might have the Python for Windows extensions already installed (are they still part of ActivePython?), so that might not count as a third party library. -- https://mail.python.org/mailman/listinfo/python-list
Re: Extraction of model and date created tag from own picture/video recordings
I have done that. The best article I have found is: https://getpocket.com/a/read/1651596570 So far I have tried exifread, exiftool, mediainfo, but looks like I will have to combine several tools in order to get the information that I need. Major problem is with the model. Looks like every manufacturer has its tags. :( -- https://mail.python.org/mailman/listinfo/python-list
Re: Extraction of model and date created tag from own picture/video recordings
On Wed, Mar 15, 2017 at 7:31 PM, wrote: > On Tuesday, 14 March 2017 19:54:18 UTC+1, MRAB wrote: >> It might be worth trying "ImageMagick". > > ImageMagick is only for pictures. :( > Do you have a suggestion for videos? What format are they in? Search the web for "read media info" and see what you find. Optionally add "python" to look specifically for libraries for Python. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Extraction of model and date created tag from own picture/video recordings
On Tuesday, 14 March 2017 19:54:18 UTC+1, MRAB wrote: > It might be worth trying "ImageMagick". ImageMagick is only for pictures. :( Do you have a suggestion for videos? -- https://mail.python.org/mailman/listinfo/python-list
Re: Extract items from string in db
On Wed, 15 Mar 2017 09:26 am, DFS wrote: > I have a SQLite database with TEXT columns containing strings of the > format: > > ['Item1: Value1 has,comma','Item2: has'apostrophe','Item3: Value3'] > > The brackets are part of the string. So in Python format, you have something like this: s = "['Item1: Value1 has,comma','Item2: has'apostrophe','Item3: Value3']" assert type(s) is str assert s[0] == "[" assert s[-1] == "]" Correct? I can't help you in getting s out of the SQLite database. Below, you tell us that you're extracting the string, but it isn't actually a string, it is a tuple. You need to fix that. > How can I unpack and access each item? > Item1: Value1 has,comma > Item2: has'apostrophe > Item3: Value3 Is the string format documented anywhere? > I tried this: > db.execute("SELECT COLUMN FROM TABLE WHERE ID = ?;",(123,)) > vals = [db.fetchone()] > for val in list(vals): > print val There's no point wrapping the output of db.fetchone() in a list. And no point converting vals to a list. db.execute("SELECT COLUMN FROM TABLE WHERE ID = ?;", (123,)) vals = db.fetchone() for val in vals: print type(val), val What does it output now? > but it just prints the string as is. > > split(",") returns "tuple object has no attribute split" If it is a tuple object, it isn't a string. It isn't both at once. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse. -- https://mail.python.org/mailman/listinfo/python-list