lastPosition = GPS.getActualPosition()
I m trying to compile a code which is about sending sms through telit module.
above statement is giving an error. I couldn't understand, GPS library is in the place where it's supposed to be and I imported it.
import SER
import MOD
import MDM
import GPS
syntaxError: invalid syntax
http://forum.sparkfun.com/viewtopic.php?f=13&t=20038
please help!!!
The post the OP referred to (in a comment -- not a great idea, #gheddo! edit your Q instead!), here, has exactly this code (I'm copying and pasting only the two relevant lines):
def get_gps():
gpspos = GPS.getActualPosition() #Read GPS position
see the problem? No indentation for the second line! Therefore, a syntax error: function bodies (and other bodies of compound statements) must be indented in Python.
It was hardly necessary to send us reading that code, you know... the code's author, John Melbourne, in the very next post in this thread says, and I quote:
Hi again Ryan,
The forum software removed the
indentation from the Python script
that I listed in my earlier mail.
You will need to re-indent the
function bodies, if and while
statements. See Flavio's original
source if your not sure how.
So that's exactly what you have to do -- re-indent the function bodies, if and while statements, and refer to Flavio Bernardotti's code if you need to for this purpose.
Also, in the future, I would strongly recommend you read at least one post later in a thread (if reading the whole thread is too much work for you...;-)...
Perhaps try some text editor with visible white-space? I've had pesky bugs like this until I turn on "Show Invisibles" in TextMate.
Related
I got bbcodepy and I'm allowed to modify it, but I can't import it in my main.py. I keep getting a SyntaxError and I don't really know what's wrong with the code because I didn't write it. I just want to tinker around a little bit and see if I can get it to fill my needs.
Here's an image pointing me in the direction of the syntax error. But I noticed the same code is written on the same line and I don't get the SyntaxError for that. Here's the code:
_URL_RE = re.compile(ur'''\b((?:([\w-]+):(/{1,3})|www[.])(?:(?:(?:[^\s&()]|&|")*(?:[^!"#$%&'()*+,.:;<=>?#\[\]^`{|}~\s]))|(?:\((?:[^\s&()]|&|")*\)))+)''')
The problem appears to be "[^\s&()]" but only the second one, not the first one. If you look closely in the code you'll see that the same thing appears twice, but I only get the SyntaxError on the second appearance. Someone enlighten me, please. I've been trying to find a decent BBCode parser for Python for a couple of days now and I believe this is the one I can modify to my needs. I can't seem to get bbcode to work as I want it to, so I'm trying this out.
Well, Python version 3.4 and above does not support a 'UR' prefix.
You need to execute your code with Python 2.7, or change to line into:
_URL_RE = re.compile(r'''\b((?:([\w-]+):(/{1,3})|www[.])(?:(?:(?:[^\s&()]|&|")*(?:[^!"#$%&'()*+,.:;<=>?#\[\]^`{|}~\s]))|(?:\((?:[^\s&()]|&|")*\)))+)''')
See also: python version 3.4 does not support a 'ur' prefix
Note: consider avoiding triple-quoted string because if you insert a newline, it can change the meaning of the regex (unless it is compiled in VERBOSE mode).
So, I recently made a Python program that I want to send to someone with them being able to execute it, but not read the code I have typed in it. Any ideas how to do it?
BTW, I want it to be irreversible
In short, here are my Parameters:
Should remain a Python file
Can't be reversed
Code should not be readable
Should still have the ability to be run
The criteria you've posted are inconsistent.
Python is an interpreted language. The entity running the language (i.e. Python interpreter) is reading your code and executing it, line by line. If you wrap it up to send to someone, their Python interpreter must have read permissions on the file, whether it's source code or "compiled" Python (which is easily decompiled into equivalent source code).
If we take a wider interpretation of "send to someone", there may be a business solution that serves your needs. You would provide your functionality, rather than the code: deploy it as a service from some available server: your own, or rented space. To do this, you instead provide an interface to your functionality.
If this fulfills your needs, you now have your next research topic.
Say, I have two files demo.py
# demo.py
from pathlib import Path
for i in range(5):
exec(Path('another_file.txt').read_text())
and another_file.txt (note the indent)
print(i)
Is it possible to make python demo.py run?
N.B. This is useful when using Page (or wxformbuilder or pyqt's designer) to generate a GUI layout where skeletons of callback functioins are automatically generated. The skeletons will have to be modified, at the same time, each iteration overwrites the skeletons -- code snippets will have to be copied back. Anyway, you know what I am talking about if you have used any of Page or wxformbuilder or pyqt's designer.
You can solve the basic problem by removing the indent:
from pathlib import Path
import textwrap
for i in range(5):
exec(textwrap.dedent(Path('another_file.txt').read_text()))
There are still two rather major problems with this:
There are serious security implications here. You're running code without including it in your project. The idea that you can "worry about security and other issues later" will cause you pain at a later point. You'll see similar advice on this site with avoiding SQL injection. That later date may never come, and even if it does, there's a very real chance you won't remember or correctly identify all of the problems. It's absolutely better to avoid the problems in the first place.
Also, with dynamic code like this, you run the very real risk of running into a syntax error with a call stack that shows you nothing about where the code is from. It's not bad for simple cases like this, but as you add more and more complexity to a project like this, you'll likely find you're adding more support to help you debug issues you run into, rather than spending your time adding features.
And, combining these two problems can be fun. It's contrived, but if you changed the for loop to a while loop like this:
i = 0
while i < 5:
exec(textwrap.dedent(Path('another_file.txt').read_text()))
i += 1
And then modified the text file to be this:
print(i)
i += 1
It's trivial to understand why it's no longer operating the 5 times you expect, but as both "sides" of this project get more complex, figuring out the complex interplay between the elements will get much harder.
In short, don't use eval. Future you will thank past you for making your life easier.
I just installed this library that scrapes twitter data: https://github.com/kennethreitz/twitter-scraper
I wanted to find out the library's functions and methods so I can start interacting with the library. I have looked around StackOverflow on this topic and tried the following:
pydoc twitter_scraper
help(twitter_scraper)
dir(twitter_scraper)
imported inspect and ran functions = inspect.getmembers(module, inspect.isfunction)
Of the four things I have tried, I have only gotten an output from the inspect option so far. I am also unsure (excluding inspect) whether these codes should go in the terminal or a scratch file.
Still quite new at this. Thank you so much for reading everybody!
Excellent question! There are a few options in trying to grok (fully understand) a new library. In your specific case, twitter-scraper, the only function is get-tweets() and the entire library is under 80 lines long.
For the general case, in decreasing order of usefulness.
Carefully read the project's description on GitHub. The ReadMe is usually the most carefully written piece of documentation.
Larger libraries have formatted documentation at http://(package-name).readthedocs.org.
pydoc module_name works when the module is installed. ``help(module_name)works in an interactive Python session after you have done animport module_name. These both work from the "docstrings" or strategically placed comments in the source code. This is also whatmodule_name?` does in iPython.
dir(module_name) also requires an import. It lists all the entrypoints to the module, including lots of odd "dunder", or double underscore, you would not normally call or change.
Read the source code. Often, this is easier and more complete than the documentation. If you can bring up the code in an IDE, then jumping around works quickly.
Also, you asked about what can be used within a script:
import os
print("Welcome, human.")
print("dir() is a function, returning a list.")
print("This has no output")
a_list = dir(os)
print("but this does", dir(os))
print("The help() command uses pydoc to print to stdout")
help(os)
print("This program is gratified to be of use.")
It seems like this library lacks proper documentation, but the GitHub page provides some usage examples to help you get started.
>>> from twitter_scraper import get_tweets
>>> for tweet in get_tweets('kennethreitz', pages=1):
>>> print(tweet['text'])
P.S. your API is a user interface
s3monkey just hit 100 github stars! Thanks, y’all!
I’m not sure what this /dev/fd/5 business is, but it’s driving me up the wall.
…
To get more information, simply look at the source code at https://github.com/kennethreitz/twitter-scraper/blob/master/twitter_scraper.py. It seems like the only function is get_tweets, which, looking at the source code, takes in two arguments, the username and the number of pages (optional, defaults to 25).
Is there any way to enable code hiding (hides the details of a class or a function just shows the definition) and line numbers in IDLE ?
Don't think so. It was discussed briefly on the mailing lists, but I don't believe anything's come out of it since.
If you're interested in code folding, look into slightly more powerful editors - Geany (GTK) and Sublime Text both support code folding.
A code/line folding feature was asked before in that Python Issue. It was closed and rejected but with good reasons.
If you know this features of IDLE you maybe do not need code folding anymore.
Module Browser:
To get an outline view of a file's classes and functions, use File => Module Browser (ALT+C).
Code Context:
To get a dynamic view of nested compound statement headers with Options => Code Context.