Re: Set Operations on Dicts

2016-02-08 Thread Grobu
On 08/02/16 17:12, Ian Kelly wrote: dict does already expose set-like views. How about: {k: d[k] for k in d.keys() & s} # d & s {k: d[k] for k in d.keys() - s} # d - s Interesting. But seemingly only applies to Python 3. -- https://mail.python.org/mailman/listinfo/python-list

Re: Set Operations on Dicts

2016-02-08 Thread Grobu
You can use dictionary comprehension : Say : dict1 = {'a': 123, 'b': 456} set1 = {'a'} intersection : >>> { key:dict1[key] for key in dict1 if key in set1 } {'a': 123} difference : >>> { key:dict1[key] for key in dict1 if not key in set1 } {'b': 456} -- https://mail.python.org/mailman/listinfo

Re: [STORY-TIME] THE BDFL AND HIS PYTHON PETTING ZOO

2016-02-03 Thread Grobu
On 03/02/16 04:26, Rick Johnson wrote: [ ... ] And many children came from the far and wide, and they would pet his snake, and they would play with his snake Didn't know Pedobear had a biographer. -- https://mail.python.org/mailman/listinfo/python-list

Re: show instant data on webpage

2016-01-29 Thread Grobu
On 26/01/16 17:10, mustang wrote: I've built a sensor to measure some values. I would like to show it on a web page with python. This is an extract of the code: file = open("myData.dat", "w") while True: temp = sensor.readTempC() riga = "%f\n" % temp file.write(rig

Re: python-2.7.3 vs python-3.2.3

2016-01-26 Thread Grobu
On 26/01/16 13:26, Gene Heskett wrote: Greetings; I have need of using a script written for python3, but the default python on wheezy is 2.7.3. I see in the wheezy repos that 3.2.3-6 is available. Can/will they co-exist peacefully? Thank you. Cheers, Gene Heskett On Debian Jessie : $ ls

Re: How to simulate C style integer division?

2016-01-23 Thread Grobu
On 23/01/16 16:07, Jussi Piitulainen wrote: Grobu writes: def intdiv(a, b): return (a - (a % (-b if a < 0 else b))) / b Duh ... Got confused with modulos (again). def intdiv(a, b): return (a - (a % (-abs(b) if a < 0 else abs(b / b You should use // here to get an

Re: How to simulate C style integer division?

2016-01-23 Thread Grobu
def intdiv(a, b): return (a - (a % (-b if a < 0 else b))) / b Duh ... Got confused with modulos (again). def intdiv(a, b): return (a - (a % (-abs(b) if a < 0 else abs(b / b -- https://mail.python.org/mailman/listinfo/python-list

Re: How to simulate C style integer division?

2016-01-23 Thread Grobu
On 22/01/16 04:48, Steven D'Aprano wrote: [ ... ] math.trunc( float(a) / b ) That fails for sufficiently big numbers: py> a = 3**1000 * 2 py> b = 3**1000 py> float(a)/b # Exact answer should be 2 Traceback (most recent call last): File "", line 1, in OverflowError: long int too large t

Re: How to simulate C style integer division?

2016-01-21 Thread Grobu
On 21/01/16 09:39, Shiyao Ma wrote: Hi, I wanna simulate C style integer division in Python3. So far what I've got is: # a, b = 3, 4 import math result = float(a) / b if result > 0: result = math.floor(result) else: result = math.ceil(result) I found it's too laborious. Any quick way?

Re: Single format descriptor for list

2016-01-20 Thread Grobu
On 20/01/16 10:35, Paul Appleby wrote: In BASH, I can have a single format descriptor for a list: $ a='4 5 6 7' $ printf "%sth\n" $a 4th 5th 6th 7th Is this not possible in Python? Using "join" rather than "format" still doesn't quite do the job: a = range(4, 8) print ('th\n'.join(map(str,a))

Re: How to union nested Sets / A single set from nested sets?

2016-01-06 Thread Grobu
On 04/01/16 03:40, mviljamaa wrote: I'm forming sets by set.adding to sets and this leads to sets such as: Set([ImmutableSet(['a', ImmutableSet(['a'])]), ImmutableSet(['b', 'c'])]) Is there way union these to a single set, i.e. get Set(['a', 'b', 'c']) ? There's a built-in "union" method fo

Re: filter a list of strings

2015-12-03 Thread Grobu
On 03/12/15 02:15, c.bu...@posteo.jp wrote: I would like to know how this could be done more elegant/pythonic. I have a big list (over 10.000 items) with strings (each 100 to 300 chars long) and want to filter them. list = . for item in list[:]: if 'Banana' in item: list.remove(it

Re: static variables

2015-12-01 Thread Grobu
Perhaps you could use a parameter's default value to implement your static variable? Like : # - >>> def test(arg=[0]): ... print arg[0] ... arg[0] += 1 ... >>> test() 0 >>> test() 1 # - -- ht

Re: Find relative url in mixed text/html

2015-11-27 Thread Grobu
On 28/11/15 03:35, Rob Hills wrote: Hi, For my sins I am migrating a volunteer association forum from one platform (WebWiz) to another (phpBB). I am (I hope) 95% of the way through the process. Posts to our original forum comprise a soup of plain text, HTML and BBCodes. A post */may/* include

Re: Screen scraper to get all 'a title' elements

2015-11-25 Thread Grobu
Chris, Marko, thank you both for your links and explanations! -- https://mail.python.org/mailman/listinfo/python-list

Re: Screen scraper to get all 'a title' elements

2015-11-25 Thread Grobu
On 26/11/15 00:06, Chris Angelico wrote: On Thu, Nov 26, 2015 at 9:48 AM, ryguy7272 wrote: Thanks!! Is that regex? Can you explain exactly what it is doing? Also, it seems to pick up a lot more than just the list I wanted, but that's ok, I can see why it does that. Can you just please expla

Re: Screen scraper to get all 'a title' elements

2015-11-25 Thread Grobu
test" class="test"' ) '"this is a test"' It matches the first quote and stops looking for further matches after the second quote. Finally, the parentheses are used to indicate a capture group : >>> a = re.search( r'"this (is) a (.+?)"', 'title="this is a test" class="test"' ) >>> a.groups() ('is', 'test') You can find detailed explanations about Python regular expressions at this page : https://docs.python.org/2/howto/regex.html HTH, -Grobu- -- https://mail.python.org/mailman/listinfo/python-list

Re: Screen scraper to get all 'a title' elements

2015-11-25 Thread Grobu
Hi It seems that links on that Wikipedia page follow the structure : You could extract a list of link titles with something like : re.findall( r'\]+title="(.+?)"', html ) HTH, -Grobu- On 25/11/15 21:55, MRAB wrote: On 2015-11-25 20:42, ryguy7272 wrote: Hello experts.

Re: What is the right way to import a package?

2015-11-16 Thread Grobu
same error. It is strange that the same content has errors depends on inside a file, or at CLI console. What is missing I don't realize? Thanks, You can try : from numpy import * from numpy.random import * HTH, - Grobu - -- https://mail.python.org/mailman/listinfo/python-list