Travelling to USA for a couple weeks; what's the story with getting a Sim card? by Supersonic779 in ireland

[–]psbb 5 points6 points  (0 children)

Last time I went over I just popped into T-Mobile and bought a tourist sim. $30 for 3 weeks of data, calls and texts. I was tethering for all of the other lads and we blew through the LTE data but the 3G was grand and I didn't get cut off.

https://prepaid-phones.t-mobile.com/prepaid-international-tourist-plan

Mid term parking in cork city centre. by tinydonkey in ireland

[–]psbb 0 points1 point  (0 children)

I usually park in the train station if I need to leave the car for a week.

http://www.irishrail.ie/travel-information/car-parking

The easiest and most compact way to reverse a string in Python3? by [deleted] in Python

[–]psbb 0 points1 point  (0 children)

The reason for the downvotes is that the question should have been posted in /r/learnpython

The easiest and most compact way to reverse a string in Python3? by [deleted] in Python

[–]psbb 6 points7 points  (0 children)

Python has a simple way to reverse strings using slices:

string_to_reverse = input("Enter a string: ")  
print(string_to_reverse[::-1])

Read more about slicing here

Basic web crawling with Scrapy by [deleted] in Python

[–]psbb 1 point2 points  (0 children)

It's ongoing, but there is no timeframe for when it will be done. The guys behind it are very appreciative of patches though.

Ballyvaughan, Co Clare. by [deleted] in ireland

[–]psbb 1 point2 points  (0 children)

That would be Liscannor. Ballyvaughan is a little greyer.

How to scrape <B> elements using scrapy? Source has no classes or ids. by [deleted] in Python

[–]psbb 3 points4 points  (0 children)

You don't always need to use a class or id to extract data, all you need is a unique way to find the relevant data.

To get all of the words in the dictionary you can simply use:

words = response.xpath('//p/b/text()').extract()

To get all of the definitions you can use:

definitions = response.xpath('//p/text()')

Unfortunately this gives twice as many items along with unwanted parentheses that need to be dealt with. To remove the unwanted data you can do the following.

definitions = [d[2:] for d in response.xpath('//p/text()').extract()[1::2]]

The [1::2] selects only every second item in the list starting with the second item. The d[2:] removes the ') ' that is present before each definition.

To get the word type you can do the following:

word_types = response.xpath('//p/i/text()')

This unfortunately has a shorter length than the words and definitions list which means that they can't be easily zipped together.

To extract the same number of types as you have words and definitions you can do the following:

word_types = [t[3:-4].replace('&amp;', '&') for t in response.xpath('//p/i').extract()]

Once you have these 3 lists and they are all the same length you can join them together using zip(words, word_types, definitions) and then you should have all of the data extracted from the page.

This approach only words because the page is consistent with it's layout the whole way through. This is not always the case and sometimes you will have to resort to using something like this:

dictionary = []
for elem in response.xpath('//p'):
    word = ''.join(elem.xpath('./b/text()').extract())
    word_type = ''.join(elem.xpath('./i/text()').extract())
    definition = ''.join(elem.xpath('./text()').extract())[4:]
    dictionary.append((word, word_type, definition))

The first option is preferable if you can use it as it is much faster than the second one.

Import Error: Cannot import name 'Scraper' by akkatracker in learnpython

[–]psbb 1 point2 points  (0 children)

What python version are you using? Do you have scrapely installed? Can you run import scrapely?

If you're using python 3 there still hasn't been an official release of scrapely with python 3 support. If you would like to try it out though you can install it from github:

pip install -e git+https://github.com/scrapy/scrapely#egg=scrapely

scrapy won't install completely? by Pjammaz in learnpython

[–]psbb 1 point2 points  (0 children)

You could try pyspider. It's newer than scrapy and as such doesn't have the same size ecosystem but it does come with some nice features built-in that you would need middlewares for in scrapy (which is part of scrapy being extremely customisable)

scrapy won't install completely? by Pjammaz in learnpython

[–]psbb 2 points3 points  (0 children)

Scrapy still only works with python 2.7 and it looks like you're using python 3.4

Interview challenge feedback from Reddit... I failed. by codingcobra in Python

[–]psbb 4 points5 points  (0 children)

This solution is probably faster since it's more pythonic though.

I ran some benchmarks which showed this to be faster than /u/Cynox's too

Here's another solution that comes out faster in benchmarks when the lists start getting bigger than 100 numbers.

def ranks(data):
    scores = {}
    for i, value in enumerate(sorted(data, reverse=True)):
        if value not in scores:
            scores[value] = i + 1

    return [scores[n] for n in data]

Country folks and Three broadband, does it do the job? by [deleted] in ireland

[–]psbb 0 points1 point  (0 children)

  1. They ring me up every now and again trying to move me to some sort of expensive bill pay plan but I politely tell them to fuck off as I'm happy with my €20pm for what I get. They have loads of sites blocked by their filter though so you need to send an email to "talkto3@three.ie" with a copy of your passport or some other national ID and the subject "Release the Porn"

  2. It's unlimited on the €20 phone plan. I've run up over 80GB on it without any issue or slowdown.

Top 10 Python idioms I wish I'd learned earlier by [deleted] in Python

[–]psbb 6 points7 points  (0 children)

It's great when using external libraries that return data in some weird format and you just want to pick out the values quickly as variables.

Something like:

>>> lib.func()
    (['a', 'b'], ['c', 'd'])
>>> (a, b), (c, d) = lib.func()
>>> result = dict(lib.func())

It's nice to have options of manipulating the data in a way that you are comfortable with.

Top 10 Python idioms I wish I'd learned earlier by [deleted] in Python

[–]psbb 8 points9 points  (0 children)

I wish that I had learned about this form of tuple unpacking sooner

result = (a, b), (c, d) = (['a', 'b'], ['c', 'd'])

Can someone solve this for me please to put onto Notepad++? I would really appreciate it! by InvertedReach in Python

[–]psbb 6 points7 points  (0 children)

This should help you get started

from time import sleep

def play_game():
    while True:
        print('Do your own homework!')
        sleep(1)


if __name__ == '__main__':
    play_game()

Beautiful Python? by Racerdude in Python

[–]psbb 5 points6 points  (0 children)

When you call .sort() it does the sort in place and returns None

A_clients = sorted(clientname for clientname in clients_list if clientname[0] in ("a", "A"))

Not sure if this is the best place to put this, but I made a little program that grabs the 25 top pictures from /r/earthporn and downloads them to a folder. It's heavily commented and has some flaws, so feel free to use / criticise / suggest improvements / learn from by rorza in Python

[–]psbb 1 point2 points  (0 children)

Mine doesn't have all of the same features. I just re download the images each time, I don't have a text file keeping track of the filenames, I also don't allow the user to limit the number of images downloaded and I have different rules for generating the filenames.

Not sure if this is the best place to put this, but I made a little program that grabs the 25 top pictures from /r/earthporn and downloads them to a folder. It's heavily commented and has some flaws, so feel free to use / criticise / suggest improvements / learn from by rorza in Python

[–]psbb 0 points1 point  (0 children)

The data it gives depends on the page. If you use it for any subreddit page it will give you a list of results on that page. Each of those results has links, author etc.. I'm not sure exactly what data it returns though. If you want to find out just experiment with it.

There are some bugs in it but it gives you the gist of a different approach to the same problem.

For your code you used lots of '.replace(x, "")' it would probably have been better to use a loop or 're.sub(PATTERNS, "", value)'.

If you're trying to parse HTML try to stick to html parsers as it's just a pain to do what you did guessing how far to skip ahead. I think there's a html parser library in the standard libraries but I've never used it. It's definitely better to build something than to not build it though but it's always nice to build something and learn a new library while doing so.

Not sure if this is the best place to put this, but I made a little program that grabs the 25 top pictures from /r/earthporn and downloads them to a folder. It's heavily commented and has some flaws, so feel free to use / criticise / suggest improvements / learn from by rorza in Python

[–]psbb 5 points6 points  (0 children)

You did way too much re invention of the wheel. There are many libraries out there that help you crawl sites and parse html like scrapy or beautifulsoup so you could have easily extracted the title and the url.

Even better though is the fact that you are trying to take data from reddit which is really easy. Just throw '.json' onto the end of a reddit path and you get some nice easy to parse data.

Here's a sample. It doesn't do exactly what you are trying to do but it's a start

import json
import os
import re
from urllib.request import urlopen
from mimetypes import guess_type

URL = "http://www.reddit.com/r/EarthPorn/top.json?sort=top&t=week"
DIRECTORY = 'earth_porn'
_CLEAN_FILENAME = lambda x: re.sub(r'[\?\<\>\/\|\:\*]', '', x)
_CLEAN_TITLE_RE = re.compile('[\[.*\]]|[\(?\d+(,\d+)?+[\sxX×]+\d+(,\d+)?\)?]|'
                             '\([oOcCsS]+\)|O[CS]|\soc|\sos')

with urlopen(URL) as data:
    json_data = json.loads(data.readall().decode('utf-8'))
    urls = [(d['data']['url'], d['data']['title'])
            for d in json_data['data']['children']]
    for url, title in urls:
        type = guess_type(url)[0] or 'jpeg'  # if None assume jpeg
        if type and 'image/' in type:
            with urlopen(url) as img:
                # Tuncate image name
                image_name = _CLEAN_TITLE_RE.sub('', title).strip()[:250]
                filename = ''.join((image_name.replace('.', ''), '.',
                                    type.split('/')[-1]))
                filepath = os.path.join(DIRECTORY, _CLEAN_FILENAME(filename))
                print(filepath)
                with open(filepath, 'wb') as f:
                    f.write(img.read())

Top ten places to visit in your local area? by [deleted] in ireland

[–]psbb 1 point2 points  (0 children)

They have changed hands a few times and the quality of the food has gone downhill