Home | Community | Message Board


This site includes paid links. Please support our sponsors.


Welcome to the Shroomery Message Board! You are experiencing a small sample of what the site has to offer. Please login or register to post messages and view our exclusive members-only content. You'll gain access to additional forums, file attachments, board customizations, encrypted private messages, and much more!

Shop: PhytoExtractum Kratom Powder for Sale   Kraken Kratom Red Vein Kratom   Bridgetown Botanicals CBD Concentrates

Jump to first unread post Pages: 1
Invisibledeadwk
00101011


Registered: 06/17/09
Posts: 8,890
Loc: Canada, eh? Flag
Python script help
    #19020771 - 10/23/13 06:06 PM (10 years, 4 months ago)

Hey so I'm writing a really basic program to check the price of bitcoins from a certain website, and everytime I run the program I get the error:

Quote:

Traceback (most recent call last):
  File "C:\Users\____\Desktop\Misc\btcpricechecker.py", line 19, in <module>
    print(btcprice[btcpricestart:btcpriceend])
TypeError: 'int' object is not subscriptable




I'm pretty new to python, but I'm guessing that the error is that the range of index values I'm looking for, is not subscriptable? Not to sure how to fix that....


code:


Quote:

##btc price checking and alerter
#webpage has  9122 index values before price start

import urllib.request
import time

bitstamp = urllib.request.urlopen("http://bitcoinity.org/markets/bitstamp/USD")
bitstampdecoded = bitstamp.read().decode("utf8")

btcprice = bitstampdecoded.find("high value'>")
print(btcprice)



btcpricestart = btcprice + 11
btcpriceend = btcprice+ 12


print(btcprice[btcpricestart:btcpriceend])





Extras: Filter Print Post Top
Offlineimachavel
I loved and lost but I loved-ftw
Male User Gallery


Registered: 06/06/07
Posts: 31,408
Loc: You get banned for saying that Flag
Last seen: 5 hours, 48 minutes
Re: Python script help [Re: deadwk]
    #19030282 - 10/25/13 09:56 AM (10 years, 3 months ago)

this line:

btcprice = bitstampdecoded.find("high value'>")

Is failing to find anything. According to the python docs (look for "string find python 3"), string.find() returns -1 on error.

When you use it later, you are trying to subscript a integer rather than a string, which causes the error. You should find out why it fails, or check for failure and do something appropriate.


--------------------
:kingcrankey: I did not say to edit my signature soulidarity! Now forever I will never remember what I said about understanding the secrets of the universe by paying attention to subtleties!

:facepalm: I'm never giving you the password again. Jerk


Extras: Filter Print Post Top
Invisibledeadwk
00101011


Registered: 06/17/09
Posts: 8,890
Loc: Canada, eh? Flag
Re: Python script help [Re: imachavel]
    #19030419 - 10/25/13 10:38 AM (10 years, 3 months ago)

Quote:

imachavel said:
this line:

btcprice = bitstampdecoded.find("high value'>")

Is failing to find anything. According to the python docs (look for "string find python 3"), string.find() returns -1 on error.

When you use it later, you are trying to subscript a integer rather than a string, which causes the error. You should find out why it fails, or check for failure and do something appropriate.





Okay, I'll try some more and see if I can fix it. I'll report back if I run into anymore difficulties.

Would I be able to convert the integer to a string? Also the price can have a decimal number sometimes, so it could either be a float, or an integer. But if I convert this to a string, whether it's an integer or float before the conversion, it should work right?


Thanks for the help!


Extras: Filter Print Post Top
Invisibledeadwk
00101011


Registered: 06/17/09
Posts: 8,890
Loc: Canada, eh? Flag
Re: Python script help [Re: deadwk]
    #19085479 - 11/04/13 01:45 PM (10 years, 3 months ago)

So I've worked on it more, and I'm running into the most strange error:

Quote:

Traceback (most recent call last):
  File "watcher.py", line 1, in <module>
    import urllib.request
ImportError: No module named request




Which is strange because I'm running python3.2, and the request module SHOULD exist...




Here is my code:


Quote:

import urllib.request
import time





def price():#this function holds the current price of bitcoins.
    webpage = urllib.request.urlopen("http://bitstamp.net")
    webpagedecode = webpage.read().decode("utf8")
    pricerange = webpagedecode.find('"last">$')#holds the index value of "last">$
    start = pricerange + 8#stores the start of the price range
    end = start + 6#stores the end of the price range
    price = float(webpagedecode[start:end])#price is stored in this variable
    return(price)#allows the price to be returned whenever the price() function is called


def btcwarningprice(btcpurchaseprice):#this function sets the warning price for when you should sell the bitcoins.
    percentage = btcpurchaseprice * 0.05 #this gets you the % value of your price.
    warningprice = btcpurchaseprice + percentage#this will add the percentage value, and your purcahse value together
    return(warningprice)#allows the warning price to be returned



print("The current price of bitcoins is", price(), "USD")#prints current price in bitcoins.


btcpurchaseprice = float(input("What price did you buy your bitcoins at xx.xx format only: ")) #this will be used to store the value at which you purchased the bitcoins
print("Please note that a warning limit is set for your bitcoin prices, which is 5% higher then what you purchased them at")

print("")


while price() > btcpurchaseprice: #compares the price of bitcoins, to the price you purchased them at.
    time.sleep(1)
    if price() <= btcpurchaseprice:
        print("The price of bitcoins is equal to or lower then the price at which you purchased them, sell sell sell!!!")
    if price() <= btcwarningprice(btcpurchaseprice):
        print("The price of bitcoins are in the warning zone, consider selling")
    if price() > btcpurchaseprice:
        print("The price of bitcoins are higher then the price at which you purchased them")





Extras: Filter Print Post Top
Offlineimachavel
I loved and lost but I loved-ftw
Male User Gallery


Registered: 06/06/07
Posts: 31,408
Loc: You get banned for saying that Flag
Last seen: 5 hours, 48 minutes
Re: Python script help [Re: deadwk]
    #19085927 - 11/04/13 03:33 PM (10 years, 3 months ago)

Just curious why did you post tons of code as in the entire file. The error seems to be the first line importing the module urllib.request. This I cant answer maybe the module urllib.request isnt in the correct folder and your .py cant find the module to import. Maybe you should take a screen shot of your directory and upload the screen shot


--------------------
:kingcrankey: I did not say to edit my signature soulidarity! Now forever I will never remember what I said about understanding the secrets of the universe by paying attention to subtleties!

:facepalm: I'm never giving you the password again. Jerk


Extras: Filter Print Post Top
Invisibledeadwk
00101011


Registered: 06/17/09
Posts: 8,890
Loc: Canada, eh? Flag
Re: Python script help [Re: imachavel]
    #19086929 - 11/04/13 07:16 PM (10 years, 3 months ago)

I don't know exactly lol, next time I'll only post the code that is in question.

Will do, I'll post the screenshot when I get a chance.


edit: The problem seems to be with the actual installation of python on my linux box.. I think I'm going to have to delete it and reinstall the python library.

Ported over the script to Windoze, and everything works. Just need to add the functionality to play a sound file, but that seems like it'll be a wee bit difficult based on my current skill level.


Edited by deadwk (11/05/13 12:01 AM)


Extras: Filter Print Post Top
Offlineimachavel
I loved and lost but I loved-ftw
Male User Gallery


Registered: 06/06/07
Posts: 31,408
Loc: You get banned for saying that Flag
Last seen: 5 hours, 48 minutes
Re: Python script help [Re: deadwk]
    #19091163 - 11/05/13 05:02 PM (10 years, 3 months ago)

Can I get some more information such as operating system you are using? language version? what you are writing this for etc.? I can try and go deeper into this, I'm not at all a python expert but when writing code you are ALWAYS accessing files, modules, input and output etc. it'd be easier to help if I knew the full extent of what you were writing and why you were writing it.

For example, what is the urllib.request module and why are you referencing/calling on it?


--------------------
:kingcrankey: I did not say to edit my signature soulidarity! Now forever I will never remember what I said about understanding the secrets of the universe by paying attention to subtleties!

:facepalm: I'm never giving you the password again. Jerk


Extras: Filter Print Post Top
Invisibledeadwk
00101011


Registered: 06/17/09
Posts: 8,890
Loc: Canada, eh? Flag
Re: Python script help [Re: imachavel]
    #19091273 - 11/05/13 05:23 PM (10 years, 3 months ago)

Well I run two operating systesm, Ubuntu 12.04 LTS, and Windows 7.
On Ubuntu I'm using Python3.2 and on Windows 7 I'm using Python3.3
I'm writing this for a personal program so I can monitor the prices of bitcoins without having to go to a website.

urllib is a python library that centers around URLs (Uniform Resource Locator)
while the request is a particular module in the urllib library.. I'm referencing with "urllib.request.urlopen()"

Basically it's requesting a webpage (request) and then opening it with urlopen(), and stores it into the variable named webpage. This isn't in human readable format, so I then do "webpage.read().decode("utf8")" which reads the data stored in webpage, and decodes it into a human readable format.


Without the request module I wouldn't be able to request the data in the website.


Extras: Filter Print Post Top
Offlineimachavel
I loved and lost but I loved-ftw
Male User Gallery


Registered: 06/06/07
Posts: 31,408
Loc: You get banned for saying that Flag
Last seen: 5 hours, 48 minutes
Re: Python script help [Re: deadwk]
    #19096049 - 11/06/13 03:42 PM (10 years, 3 months ago)

Quote:

thedeadwalkk said:
Well I run two operating systesm, Ubuntu 12.04 LTS, and Windows 7.
On Ubuntu I'm using Python3.2 and on Windows 7 I'm using Python3.3
I'm writing this for a personal program so I can monitor the prices of bitcoins without having to go to a website.

urllib is a python library that centers around URLs (Uniform Resource Locator)
while the request is a particular module in the urllib library.. I'm referencing with "urllib.request.urlopen()"

Basically it's requesting a webpage (request) and then opening it with urlopen(), and stores it into the variable named webpage. This isn't in human readable format, so I then do "webpage.read().decode("utf8")" which reads the data stored in webpage, and decodes it into a human readable format.


Without the request module I wouldn't be able to request the data in the website.




Ubuntu 12.4 LTS huh? I'm running that also.

Ok, so let me get this right, you wrote a file that runs a script to request code from a web page, the code is in python, it opens a page from a specific url, which has a python file, not readable, but downloads it, then the code you wrote is supposed to interpret it huh? Well I was a little stumped on this and myself am not a python expert, I can read python enough to see what errors returned when trying to run it are returned, and could see what the issues with what lines were being returned. This is simply what I mentioned, the issues and how to address and look for the correct way to re write the line or lines in question.

But as far as I could see, the code looked as though it worked fine for the most part, as far as I could tell, and that this is more of a configuration issue with the web requests and maybe the version of python you are working with then necessarily that you code wrong, at least if you keep re writing it and getting the same errors. So I asked a friend of mine who writes python and he somewhat agreed, he wrote this:

"He replied, I read it. The import works just great on my linux box with python 3, but python 2 is the default, which I bet is why he's having problems; if he runs 'python' from the command-line, he'll get python 2. He'll need to run 'python3' to get python 3. Check if he's doing that.

Other than that, I don't really know what's up. This is more system administration than programming."


Does any of this help?


--------------------
:kingcrankey: I did not say to edit my signature soulidarity! Now forever I will never remember what I said about understanding the secrets of the universe by paying attention to subtleties!

:facepalm: I'm never giving you the password again. Jerk


Extras: Filter Print Post Top
InvisibleCyclohexylamine
Turn on, Tune in, Drop out
 User Gallery


Registered: 09/08/10
Posts: 14,327
Re: Python script help [Re: imachavel]
    #19105350 - 11/08/13 12:12 PM (10 years, 3 months ago)

Quote:

imachavel said:
"He replied, I read it. The import works just great on my linux box with python 3, but python 2 is the default, which I bet is why he's having problems; if he runs 'python' from the command-line, he'll get python 2. He'll need to run 'python3' to get python 3. Check if he's doing that.






Yeah this would be the issue with the request module not being available.
In older version of python (eg. 2.7.6, most likely what is on your system) you would just include urllib2, but as of python 3 the library has been split into urllib.request and urllib.error


--------------------
Yes this is tymo - I just changed my name

Have you ever had a dream that you were so sure was real? What if you were unable to awake from that dream? How would you know the difference between that dream world and the real world?


There is NOTHING better than feeling that warm dissociative fuzz creeping up your body from IM K
Something abut that anaesthetic rush... :inlove:

Qualitative Research Chemical Effects and Experiences
The Wonderful World of Methoxetamine
The 3-Meo-PCP Chapters, Part One


Extras: Filter Print Post Top
Invisibledeadwk
00101011


Registered: 06/17/09
Posts: 8,890
Loc: Canada, eh? Flag
Re: Python script help [Re: Cyclohexylamine]
    #19106903 - 11/08/13 05:41 PM (10 years, 3 months ago)

@imachavel:

Yea exactly! The code interprets it, and picks out a specific substring within the string and then uses that for comparison.

Yea that helped! Thanks man :laugh:


@tymoteusz3:

Good to know! I find it strange though because I downloaded python3.2 from the Ubuntu Software Center, so shouldn't it run with the python 3 library as default?


Extras: Filter Print Post Top
InvisibleCyclohexylamine
Turn on, Tune in, Drop out
 User Gallery


Registered: 09/08/10
Posts: 14,327
Re: Python script help [Re: deadwk]
    #19106914 - 11/08/13 05:45 PM (10 years, 3 months ago)

Quote:

thedeadwalkk said:

@tymoteusz3:

Good to know! I find it strange though because I downloaded python3.2 from the Ubuntu Software Center, so shouldn't it run with the python 3 library as default?




Nope, since python 2.6.7 is also installed it is set to default. You would have to uninstall python 2.6.7 or change the link to the binary referred to by the "python" command.


--------------------
Yes this is tymo - I just changed my name

Have you ever had a dream that you were so sure was real? What if you were unable to awake from that dream? How would you know the difference between that dream world and the real world?


There is NOTHING better than feeling that warm dissociative fuzz creeping up your body from IM K
Something abut that anaesthetic rush... :inlove:

Qualitative Research Chemical Effects and Experiences
The Wonderful World of Methoxetamine
The 3-Meo-PCP Chapters, Part One


Extras: Filter Print Post Top
Invisibledeadwk
00101011


Registered: 06/17/09
Posts: 8,890
Loc: Canada, eh? Flag
Re: Python script help [Re: Cyclohexylamine]
    #19106929 - 11/08/13 05:49 PM (10 years, 3 months ago)

Ah okay, I'm guessing they do that for backwards compatability?


Extras: Filter Print Post Top
InvisibleCyclohexylamine
Turn on, Tune in, Drop out
 User Gallery


Registered: 09/08/10
Posts: 14,327
Re: Python script help [Re: deadwk]
    #19107423 - 11/08/13 07:52 PM (10 years, 3 months ago)

Quote:

thedeadwalkk said:
Ah okay, I'm guessing they do that for backwards compatability?




It's just because 2.6.7 is installed by default on Ubuntu - because the majority of scripts are still using an older version. Installing a newer version doesn't overwrite the link because that would break a lot of programs - but linux allows you to update the shell command to a newer binary using update-alternatives command.


--------------------
Yes this is tymo - I just changed my name

Have you ever had a dream that you were so sure was real? What if you were unable to awake from that dream? How would you know the difference between that dream world and the real world?


There is NOTHING better than feeling that warm dissociative fuzz creeping up your body from IM K
Something abut that anaesthetic rush... :inlove:

Qualitative Research Chemical Effects and Experiences
The Wonderful World of Methoxetamine
The 3-Meo-PCP Chapters, Part One


Extras: Filter Print Post Top
Invisibledeadwk
00101011


Registered: 06/17/09
Posts: 8,890
Loc: Canada, eh? Flag
Re: Python script help [Re: Cyclohexylamine]
    #19107539 - 11/08/13 08:14 PM (10 years, 3 months ago)

Oh cool, that's pretty nifty! I don't think i'll update the shell command, I'll just stick with using python3


Extras: Filter Print Post Top
Jump to top Pages: 1

Shop: PhytoExtractum Kratom Powder for Sale   Kraken Kratom Red Vein Kratom   Bridgetown Botanicals CBD Concentrates


Similar ThreadsPosterViewsRepliesLast post
* GUI development in Python deadwk 880 5 12/14/13 01:20 AM
by deadwk
* linux shell script help OJK 986 7 05/23/06 12:21 PM
by OJK
* IPv4 subnetting script (python) deadwk 827 1 11/06/13 01:24 PM
by deadwk
* Scripting Languages: Ruby vs. Python Evolving 1,178 8 02/14/04 08:57 PM
by djfrog
* Learning How to program with Python.
( 1 2 all )
teknix 3,439 32 10/16/13 02:12 AM
by teknix
* Who wants to teach me python? LuNaTiX 784 8 01/10/09 11:41 AM
by Baby_Hitler
* Learning more advanced Python, where to go? deadwk 342 2 12/24/13 07:07 PM
by deadwk
* About mining Bitcoins (the deep web currency)
( 1 2 3 4 all )
Zorro 63,479 71 05/13/12 02:53 AM
by DieCommie

Extra information
You cannot start new topics / You cannot reply to topics
HTML is disabled / BBCode is enabled
Moderator: trendal, automan, Northerner
1,427 topic views. 0 members, 2 guests and 3 web crawlers are browsing this forum.
[ Show Images Only | Sort by Score | Print Topic ]
Search this thread:

Copyright 1997-2024 Mind Media. Some rights reserved.

Generated in 0.027 seconds spending 0.004 seconds on 12 queries.