« first day (1348 days earlier)      last day (3616 days later) » 

9:00 AM
actually, I'm going to go to sleep for a bit (or try to), @Ffisegydd you have the helm as it were
 
Right. Coup time.
 
That can't wait until tomorrow? :p
 
Urgh...typo..I meant... soup time ...
 
I was worried about your ambitions there :)
You have the con. I'm going to my ready room (ie: bed)
bbi4
rbrb all
 
RED ALERT
actually more of a puce alert
I'm right in thinking I can have a list in a dictionary, right?
 
9:11 AM
Depends.
 
We could go to Red alert but it does mean changing the bulb?
 
You can have it as a value but not a key
 
Like dict = { "Foo":["bar","woo"] }
 
@ElendilTheTall yes you can have that
Also don't call a dictionary "dict"
 
sweet
stand down all hands
@Ffisegydd yeah, that was just for illustrative purposes
I'm not a total newb
 
9:13 AM
It's a common mistake that even non-total newbs make :P
 
<surreptitiously changes dictionary name>
 
And it's a total pig to debug.
 
and just to refute my previous statement, how would I reference, say, the second value in the list stored in the dictionary?
If I wanted to set a variable to "woo" for example
 
d = { "Foo":["bar","woo"] }
print(d['Foo'][1])
so d['Foo'] returns a list object and you want the 2nd (1) element
 
cool, that makes sense
I am coding a little Periodic table quiz
so storing the elements in a dictionary with their atomic number and symbol
 
9:19 AM
Hmmm maybe think about a namedtuple
Let me write a quick example up
 
okey dokey
though it also an exercise in manipulating dictionaries :)
this is the danger of asking questions - someone more experienced will come along and say "You don't want to do it like that, use this shiny function from this framework!" with the result that one doesn't learn the fundamentals
 
namedtuple is part of the standard library (albeit a more advanced part)
from collections import namedtuple

Element = namedtuple('Element', ['symbol', 'atomic_number'])

hydrogen = Element._make(['H', 1])

print(hydrogen.symbol) # 'H'
print(hydrogen.atomic_number) # 1
So what you effectively do is create an "Element" object that can hold data about your elements. In the example I've made an object from it to describe hydrogen
Then rather than having hydrogen[0] for the symbol you can do hydrogen.symbol
 
i see
so it's kind of a dictionary's dictionary
it is to dictionaries what dictionaries are to lists
 
Well it's a tuple but a tuple where the elements have names rather than just indexes
 
9:26 AM
Better example:
(Will only work with Python 3 due to the print statement though)
from collections import namedtuple

Element = namedtuple('Element', ['symbol', 'atomic_number'])

names = ['Hydrogen', 'Helium', 'Lithium']
symbols = ['H', 'He', 'Li']
atomic_numbers = [1, 2, 3]

elements = {name:Element._make([symbol, atomic_number]) for name, symbol, atomic_number
            in zip(names, symbols, atomic_numbers)}

for key in sorted(elements):
    print(key, elements[key].symbol ,elements[key].atomic_number, sep=' : ')
    # Helium : He : 2
    # Hydrogen : H : 1
    # Lithium : Li : 3
So there you've got 3 lists initially (your raw data if you like) holding the names, symbols and atomic_numbers.
Then you create a dictionary called elements
 
what is zip?
 
zip is a python function that effectively inverts some iterables. So say you have two lists [1, 2, 3] and [4, 5, 6]
And you do zip(a, b)
Then you'll get [(1, 4), (2, 5), (3, 6)]
zip is actually a really important and useful function in Python. So while a lot of the stuff I said up there is quite complicated if you remember nothing else, remember zip.
It'll save your bacon.
 
so in the example above, zip is producing ['Hydrogen','H','1']?
 
Yes exactly! :D
Well it's producing it for each of the elements
 
cbg
 
9:32 AM
So it also produces one for helium etc
 
hang on, why can't i delete my own message?
 
[('Hydrogen', 'H', 1), ('Helium', 'He', 2), ('Lithium', 'Li', 3)]
 
@Volatility edit and put a space in there
 
@Vol what do you want deleting? The wiki link?
 
9:33 AM
Would this be better then zip?
In [7]: [(x, y) for x in xrange(3) for y in xrange(3)]
Out[7]: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
 
@Ffisegydd yeh, thanks
 
1 message moved to Trash can
@RickyWilson it does something different
 
@RickyWilson now that's cartesian product
 
@Ffisegydd did you have a chance to try magic?
 
@Jon no I let it download last night, will try it tonight
 
9:36 AM
k... ugh, forget I'm meant to be getting sleeping and not doing stuff :(
 
Anyway @ElendilTheTall I realise I've thrown a lot of stuff at you (especially as you stated you were just learning things) so sorry about that :P but it's nice to push yourself occasionally :D
 
guys does anyone here have 32-bit python installed on windows?
 
I hope not
 
@Ffisegydd so I can still reference an element thus hydrogen.symbol etc?
 
If you define hydrogen then yes, but if it's in a dictionary then you'd need elements['hydrogen'].symbol
 
9:42 AM
right, right
 
class Memoize:

def __init__(self, fn):
self.fn = fn
self.memo = {}

def __call__(self, arg):
if arg not in self.memo:
self.memo[arg] = self.fn(arg)
return self.memo[arg]

@Memoize
def fib(n):
a,b = 0,1
for i in range(n-1):
a,b = b,a+b
yield a

Can you guess what this does?
 
I'd imagine it caches the results of a previous call.
 
tip: when you're posting a block of code, post it as it's own message, without any unindented things
otherwise the indentation gets mangled
 
I was about to ask about that
 
@Ffisegydd apart from the print(), is there anything else in that code that needs 3?
 
9:45 AM
No. And if you want to fix the print then just remove sep = ' : '
 
I'm slightly confused - if the elements are stored in the dictionary elements, what is the point of the namedtuple Element?
is each entry in elements a namedtuple?
 
Yes exactly
 
ah, gotcha
what is name: doing?
 
You mean the dictionary comprehension? (where I define elements?)
 
hang on, what's ._make?
 
9:48 AM
yeah elements = {name:Element etc
 
Ok so that is just a one-line method of doing the following
 
ah, that's setting the name of the namedtuple
 
R: 12, 0: Too many instance attributes (8/7) (too-many-instance-attributes)
W: 61,14: Unused variable 'dirs' (unused-variable)
https://github.com/Ricky-Wilson/disccrawler/blob/master/disccrawler.py
How do i fix these errors ?
 
@ElendilTheTall no, it's setting the key in the dictionary for the namedtuple
 
elements = {}
for name, symbol, atomic_number in zip(names, symbols, atomic_numbers):
    elements[name] = Element._make([symbol, atomic_number])
So within a dictionary you have a series of key-value pairs. In this case the key is name (and is "hydrogen", "helium", "lithium", etc) and the value is a namedtuple which contains some information about the symbol and atomic number
 
9:52 AM
@Ffisegydd instead of using Element._make, couldn't you just do Element(symbol, atomic_number)?
 
@JonClements oh, wasn't looking to ask anything, just checking in ;)
 
@Volatility possibly, I can never remember the namedtuple syntax
 
@Ffisegydd that's reassuring ;)
Element = namedtuple('Element', ['symbol', 'atomic_number'])
why is the first parameter in the namedtuple 'Element' rather than 'name'?
 
docs.python.org/3.4/library/… so first you define Element = namedtuple('Element', ['symbol', 'atomic_number']) and you could (effectively) think of that as a factory for producing namedtuples with that information
 
this throws up an aside: am i better off jumping straight in at Python 3 rather than 2.7?
 
9:55 AM
Yes in 95% of the cases. If you don't mind me asking: what do you do/what do you want to use Python for?
 
purely my own edification at present
 
Ah ok. Use Python 3 then.
I'd only suggest using Python 2 if you were a scientific researcher or similar as some science modules only support 2.7 (though a lot now support both).
They're very similar anyway, so if you can use 3 then you can use 2 (with some small rule changes)
 
i am not a programmer by trade, but I make use of various bits and bobs at work - php mainly. I generally learn stuff piecemeal as and when I need it, but I want to try and learn something from scratch, and python has a pleasing lack of ;s and $s :)
is there any issue with having 2.7 installed alongside 3?
or is it better to uninstall 2.7?
 
not that I know of
 
@ElendilTheTall It may be irregular and confusing to have both, as to figuring out which version is running. It's definitely not a big issue however. My tentative suggestion is to uninstall 2, but really do whatever you think is best.
 
10:10 AM
Generally you can rename your 3.4 installation to python3 and then run either python or python3 as suits your needs.
Some people (Martijn I believe in particular) can have 7-8 different python installations
 
Are we getting 2.7.7? I thought Py 2 is deprecated
 
@Ffisegydd Absolutely, normal use case it should be fine. My recommendation comes from the traumatic experience of the different versions, minor versions and environments all being different on the computers at my university.
 
@thefourtheye bug fixes and the like I assume
 
Oh, so no major changes... Okay
 
Possibly some more Python 3 goodies will be backported? Dunno
 
10:15 AM
cool, thanks gents
I must say, you are all more helpful than the denizens of the PHP room
but then perhaps using PHP turns you into a bitter, twisted husk
 
No major upgrades. But 2.7.7 does have set and dictionary comprehension now, which would have been very useful this semester...
 
installing 3 now
 
@Kamikai Even 2.7.6 has them... I think they were introduced much before. I remember using them in 2.7.3
 
so if I just change my environment variable to c:\python34 running python will automatically use 3, right?
sweet, my periodic table quiz works so far
 
@thefourtheye Hmm, It appears you are right, I just did a test in 2.7.6 and they work fine. I guess the link I posted mentions it's all in python 2.7, not 2.7.7 specifically
 
10:26 AM
what's the best way to grab a random entry from a dictionary?
 
@ElendilTheTall look up the python random module
 
jeez, you mean I have to think for myself? ;)
ooo, it works
that was almost too simple
 
@ElendilTheTall Haha, it's a very common feeling with python. It feels like executable pseudocode at times.
 
I'm tempted to just try "generate periodicTableQuiz" and see what happens...
 
@ElendilTheTall I take it you've tried "import antigravity" ?
 
10:33 AM
@Kamikai dare I?
 
@ElendilTheTall One of pythons best easter eggs in my opinion
 
aside from, of course, from __future__ import braces
 
@Volatility Absolutely, that was hilarious when I found that :)
 
haha, that's great
hmmm
why am I getting an error with this?
answer = int(raw_input("What is the atomic number of ",random_element))
 
it's input in python 3
 
10:40 AM
ah
hmm, same problem
expects 1 argument
fair enough, but how am I supposed to insert a variable into that input string?
 
oh, that's cause input doesn't work like print, so you have to actually concatenate the strings
 
ah ha
got it
thank you
hmmm
ok, next problem - I use this random_element = random.choice(elements.keys()) to grab a random element from the dictionary
but random_element is then a string, so I can't refer to it to get the atomic_number
I can't say random_element.atomic_number
 
it's a key to the dictionary though
 
You can look it back up in the dictionary. So for example you could have random_element = random.choice(elements.items())
and then answer = int(input("What is the Atomic Number of {} with mass {}".format(*random_element))
 
I propose rand_element, element_props = random.choice(elements.items())
@Kamikai you mean elements.values() i think
@ElendilTheTall in any case, with .items(), you can then do element_props.atomic_number
 
10:50 AM
@Volatility For my case I was assuming it was the element name mapping to its weight, but if the name and mass where the values mapped to via teh atomic number then yes, values is what would be needed.
 
ah, ok
 
What I want to do is, once the answer has been entered, do a simple if statement to check if it's correct
 
if int(input("Bla")) == element_props.atomic_number:
 
ok, that works
out of interest, why can't I use random_element.atomic_number in the comparison?
why is random_element different to element_props?
aren't they both initialised with the same thing?
 
to answer your first question, it's because random_element only stores the key, not the thing assosciated with that key (i.e. the namedtuple)
 
11:01 AM
ok
so why does random_element only store the key, but element_props store the associated info?
 
as for your second, there is something called "tuple unpacking" in Python where you can assign different variables to different items in a sequence
so if you do a, b = (1, 2), then a == 1 and b == 2
because .items() gives a (key, value) pair, we can unpack it
 
so could I also use element_props.symbol? (assuming I removed the int() from the input)?
 
yeah
 
cool
so when you write a, b = tuplename.items() you are saying 'assign a to the key, and b to the items associated with that key'?
 
erm, maybe ".items() gives a (key, value) pair" was a bit misleading
.items() is a dictionary method which returns an iterator of the (key, value) pairs in the dictionary
because you used random.choice, you only got one pair
but what's tuplename supposed to represent?
 
11:10 AM
sorry, dictname would have been more appropriate
 
ok
so you would have to use a, b = random.choice(dictname.items()) for a random pair
 
which is essentially what I'm doing
 
but yeah, a would be the key and b would be the value
 
ok, now we're having some fun
i've put my question/answer code in a while loop to ask multiple questions
all fine and dandy, except
 
you get repeats?
 
11:15 AM
there's nothing stopping random.choicegrabbing the same element multiple times
right
obviously with the whole table in there that would be less of an issue, but still...
I'm thinking add each choice to a list and check the list each time?
if choice in list then choose again
otherwise proceed
 
what i would suggest is to use random.sample (i think that's the right one)
 
or use some pre-done function like random.sample() ;)
 
exactly :)
 
Hi
 
yeah, so use something like element_list = random.sample(elements.items(), 10)
@Enissay hello :)
 
11:18 AM
Using beautifulSoup, is there a way to select an element having NO childs... It seems not to be possible using basic css
 
erm, I don't know beautifulSoup, but I dabble in CSS - what are you trying to do?
 
@Volatility hmmm, but then that throws up new problems
 
cbg all :-)
 
@ElendilTheTall I renamed the list to element_list, if that was the problem
@Zero cbg!
 
@Volatility how do I now, for example, insert the element name into my question input string?
how do I compare the given answer to the correct one?
I can't use element_list.atomic_number, for example
 
11:22 AM
you would iterate over the list using:
 
How to select the second p element having no children in pastebin.com/ttpkz2fc
 
for name, (symbol, atomic_number) in element_list:
    if int(input("Bla " + name)) == atomic_number:
        print("Correct!")
 
Hint: it will not be number two everytime xD
 
@Enissay sorry, can't help you there :/
 
@Volatility thanks
I have done it my way just because it makes sense to me and I am building my knowledge-scaffolding as it were
walk before you can run and all that
previous = []
turns = 3
while turns > 0:
	random_element, element_props = random.choice(elements.items())

	if random_element not in previous:
		previous.append(random_element)
		print previous
		answer = int(input("What is the atomic number of "+random_element+"?: "))

		if answer == element_props.atomic_number:
			print ("Correct!")
		else:
			print ("Wrong!")

turns -= 1
obviously print previous is just for debugging
 
11:35 AM
hang on, is this python 2 or 3?
 
ok, should be print(previous) (note brackets)
 
hmmm
perhaps I need to restart now i've changed my environment variable to use 3
is there a command to display the current version of python being used?
ah, 'python'
yeah, 2.7
 
restart needed methinks, brb :)
 
11:39 AM
@JonClements soup.decode(formatter='html') and all the special chars will turn into &...; -- good to know :)
 
@Volatility well, the switch to 3 has caused a f$#k up
 
oh dear
 
cabbage
 
TypeError: 'dict_items' does not support indexing
it's an error in the random module
 
try list(elements.items())
.items() is an iterator rather than a list in python 3
 
11:44 AM
random.choice(list(elements.items())?
 
cabbage @Lev!
with an extra ) on the end :)
 
sweet, that works
hmmm
 
cabbage
 
ok
I have to ask
what is cabbage
in the context of this room
 
11:57 AM
^^
 
Cbg
 
Melon
not to be confused with the Sindarin mellon
 
nope
 

« first day (1348 days earlier)      last day (3616 days later) »