How do python scripts work? - python

I just learned how to read and send emails using python and I read that you can create a python script that can read someone's twitter or facebook and send you an email whenever a specific person posts something on twitter or facebook, but how does this work?
What is the difference between a script and just a regular program? I don't think that if someone is doing something like this that they would need to have their computer on at all times and have the python program running in the background, or is that what is happening?

A python script is a kind of program.
To make what you're talking about, you'd need to either have some kind of notification when someone tweets or something to check constantly for new content.
Either way, the computer will have to be on at all times (for that, you could use a Raspberry Pi for example, so you don't take that much power). (EDIT : you can of course also have a server doing that for you, but it's still a computer running at all times.)
You'll need to get your hand onto the Twitter API documentation. There is probably a python wrapper.
(EDIT:Tweepy - Would be the aforementioned python wrapper)

Related

GRPC: Sending messages from Python to C#, best method?

I am working on a project that just scrapes data from 3 devices (2xserial and 1xssh). I have this part implemented no problem.
I am now heading towards the second part where I need be be able to send the data I need using protobuf to the clients computer where they will receive and display on their own client.
The customer has provided examples of their GRPC servers, and it's written in C#.
Currently, for security reasons, our system uses RedHat 8.3 and I am using a SSH Protocol Library called Paramiko for the SSH part. Paramiko is a Python library. Also the machine I am extracting data from only runs on Linux.
Here are my main questions, and I apologize if I got nowhere.
1.) The developer from the client side provided us with a VM that has a simulator and examples written in C# since their side was written in C#. He says that it's best to use the C# because all examples can be almost re-used as it's all written, etc. While I know it's possible to use C# in Linux these days, I've still have no experience doing so I don't know how complicated/tedious this can get.
2.) I write code in C# and wrap all the python code, which is also something I've never done, but I would be doing this all in RedHat.
3.) I keep it in python because sending protobuf messages works across languages as long as it is sent properly. Also from the client side, I'm not sure if they will need to make adjustments if receiving protobuf messages written in Python(I don't think this is the case because it's just serialized messages, yea?).
Any advice would be appreciated. I am looking to seek more knowledge outside my realm.
Cheers,
Z
If you're happy in Python, I would use option 3. The key thing is to either obtain their .proto schema, or if they've used code-first C# for their server: reverse-engineer the schema (or use tools that generate the schema from code). If you only have C# and don't know how to infer a .proto from that, I can probably help.
That said: if you want to learn some new bits, option 1 (using C# in your system) is also very viable.
IMO option 2 is the worst of all worlds.

Demo a CLI program without a notebook

A client has asked that I put together a Python program to demonstrate some core concepts of a possible ML app. We agreed I would make a CLI for now just to show how it would work and later we would make a web interface.
I wrote up a proposal and he read it and agreed. I'm now 100% done with this project and would like to collection the 2nd 1/2 of the payment.
Originally he said a video of the functionality was fine, now he is saying it's not and he wants to 'play with it'.
I don't want to give him the code, and I really don't want to do any more work for this client until he pays the rest of the money.
Here's my question: Is there a way to publish a Python CLI online without:
Doing a lot more work (like making a web interface).
Exposing the source code.
If he had to SSH into somewhere that might work.

How to use Python to make Minecraft Java Deep Learning bots

Okay I have an idea that make be theoretical. I was looking at this paper, and I saw that they used something called Malmo to interact with Minecraft. I took a quick look at this and I saw this was a offline thing. I want to make a program to directly interact with Minecraft Java. I thought about using some library to read pixel data and using the keyboard library for input. This would work of course but I want to also fetch things like player position, inventory, and other things. I would ideally want this to work client side. So my final question is how would I be able to test neural network Minecraft bots with python on pure Minecraft Java? Would I need to make a Forge or Fabric client?
I don't think this is possible in Python but maybe in JavaScript/ Node.js with a library called Mineflayer. you can't directly send keystrokes and mouse input from Python because Minecraft will receive it as a digital input and simply won't respond properly. here is the Mineflayer Github where you can find how to use it and various addons to MineFlayer such as MineFlayer-pathfinder or MineFlayer-navigate.

How can I upload my python trading bot to the internet for auto-trading?

I have written a python code that I can run it on my computer. It gets stock data from a website by GET request. Then it does some calculations on upon these online data and decides to send a buy/sell request to my broker's account.
All of these works fine on my computer, but I like to upload my code on a web server to preventing electricity/internet disconnection. I think I should upload my code on a virtual server, but this is all the thing I know and I don't have more experience or knowledge about it. I don't know if it is what I need, how can I do that?
In my experience of making some simple web pages, we should put our main code inside the public.html file? So should I buy a web host and wrap my python code inside a .html file?
Sorry if they are simple/stupid questions but I couldn't find any answer or guidence for doung what I want, so I asked here!
With the info you provide I can't really be sure how your script is ran.
I presume you run it by shell in that case a simple vps should suffice.
No need to mess around with a webserver.
Maybe look at Digital Ocean. They are a market leader and the simple $5 option probably enough for your purpose.
When you set it up you can connect to it with ssh (preferably with a ssh key because you are working with financial data) and setup your python script as if it were your own computer.
Ps. I don't work for digital ocean or get money from the link. It is just an example, any vps will work.

Streaming the result of a command back to the browser using Twisted and Comet

I'm writing an application that streams the output (by this I mean both sys.stdout and sys.stderr) of a python script excited on the server, in real time to the browser.
The users on the site will be allowed to select the script to run, excite and kill their chosen script, and change some parameters, so I will need a different thread per user on the site (user A can start, stop and change a script, whilst user B can do the same with a different script).
I know I need to use comet for the web clients, and seeing as the rest of the project is written in python, I'd like to use twisted for the server, however I'm not really sure of what I need to do next!
There are a daunting number of options (Divmod Mantissa, Divmod Nevow, twisted.web, STOMP, etc), and some are better documented that others, making the whole thing rather tricky!
I have a working demo using stompservice on orbited, using Orbited.TCPSocket for the javascript side of things, however I'm starting to think that STOMPs channel model isn't going to work for multithreading, multi-running scripts (unless I open a new channel per run, but that seems like the wrong use of the channel model).
Can anyone point me in the right direction, or some sample code I can learn from?
Thanks!
Nevow Athena is a framework specifically for AJAX and COMET applications and in theory is exactly the sort of thing you are looking for.
However, I am not sure that it is well used or supported at this time - looking at mailing list traffic and google search results suggests that it may not be.
There are a couple of tutorials you could look at to help you decide on it:
one on the 'official' site: http://divmod.org/trac/wiki/DivmodNevow/Athena/Tutorials/LiveElement
and one other that I found:
http://divmodsphinx.funsize.net/nevow/chattutorial/part01/index.html
The code for the latter seems to be included in the Nevow distribution when you download it under /doc/listings/partxx (I think...)
You can implement a very simple "HTTP streaming" by keeping the http connection open and appending javascript chunks that update the dom contents. This works since the browser evaluates the "script" chunks as they arrive.
I wrote a blog entry a while ago with a running example using twisted and very few lines of javascript: Simple HTTP streaming with Twisted & Javascript
You can easily mix this pattern with a publisher/subscriber pattern to make it multiuser, etc. I use this pattern to watch live log streams via web.
An example of serving for long-polling clients with Twisted is slosh. This might not be what you want, but because it's not a large framework, it can help you figure out how to use Twisted.

Categories