Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Tech C**P
What is LPUSH in REDIS: Insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations. When key holds a value that is not a list, an error is returned.…
O(1) or O(n) access time?

O(1) means that it takes a constant time, like 14 nanoseconds, or three minutes no matter the amount of data in the set.

O(n) means it takes an amount of time linear with the size of the set, so a set twice the size will take twice the time. You probably don't want to put a million objects into one of these.

#time #complexity #time_complexity
The simplest way to generate hour, minute, seconds from seconds in python is divmod:
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print "{}:{}:{}".format(h, m, s)

#python #divmod #format
Removing docker network in swarm mode gives error below:
Error response from daemon: network demo_net has active endpoints

In order to remove the network you need to remove dangling containers that are bound to the network, to see those containers:
docker network inspect demo_net

In Containers section you would see name of those containers. Grab the name of the container and use disconnect as below to remove it from the network:
docker network disconnect -f demo_net NAME_OF_YOUR_CONTAINER

#docker #network #disconnect #swarm
How to replace multiple characters in a string using python?

As you may already know there is a method for string called replace that replaces 1 character with a new given character:
'+98 21 445 54 12'.replace('+', '')
# output is 98 21 445 54 12

But what if you want to replace both + and [SPACE] inside of the string? The answer is simple just chain the methods:
'+98 21 445 54 12'.replace('+', '').replace(' ', '')

Here output would be 98214455412.

#python #string #replace #multiple_replacement
Tech C**P
How to replace multiple characters in a string using python? As you may already know there is a method for string called replace that replaces 1 character with a new given character: '+98 21 445 54 12'.replace('+', '') # output is 98 21 445 54 12 But…
If the replacements are going to be more in number, you can do this in this generic way:
strs, replacements = "abc&def#ghi", {"&": "\&", "#": "\#"}
print "".join([replacements.get(c, c) for c in strs])
# abc\&def\#ghi

#python #join #replace
Fraud Detection on credit card using Apache Kafka KSQL:

CREATE TABLE possible_fraud AS
SELECT card_number, count(*)
FROM authorization_attempts
WINDOW TUMBLING (SIZE 5 SECONDS)
GROUP BY card_number
HAVING count(*) > 3;

The above query creates a moving window in 5 seconds timeframe and check whether there are more than 3 authorization attempt on a
specific credit card, real time!

KSQL is a good fit for identifying patterns or anomalies on real-time data. By processing the stream as data arrives you can identify and properly surface out of the ordinary events with millisecond latency.

#apache #kafka #ksql #fraud_detection
How to check whether a socks server working or not?

Let's say you have run a socks server on port 8888. Now in order to test the server, you just need to use cURL! You need to provide --socks5 parameter to cURL as below:
curl --socks5 localhost:8888 binfalse.de

If you see some HTML stuff, your server is up & running, but if you see the below error it seems the server is not working properly:
curl: (7) Failed to connect to localhost port 8888: Connection refused

The above error says that port 8888 is not available, you may see different errors based on the server configuration stat. Hope it helps.

#socks5 #socks #socks_server #curl
If you are working in docker and have utf8 problems like UnicodeDecodeError exceptions. And you are scratching your head like me and encode & decode does no good for the problem, use setdefaultencoding:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

NOTE: it is discouraged to use the above function. As I have mentioned docker which its OS is isolated, I aware of what I'm doing. In case you are in doubt DO NOT USE IT specially on host OS!

#python #encoding #utf8 #encode #decode
As you may remember we have explained redis pubsub in which someone subscribe to a specific channel and then another console publish messages to that channel. The publisher has no idea who is listening on the other side, it just publish messages which is received.

Today we are gonna give a python example of the redis pubsub using threading and redis python module. Take a look at the code in the link below:

https://gist.github.com/alirezastack/ff2515cc434360f544d8a9341155947e

I prefer not to clutter the post by pasting the whole script here. :)

subscribe method is used to subscribe to a given channel, here it is called test. start method is used as part of the threading
module which causes run method to be called. Both run and start is Thread super class methods. When you run the script it will subscribe to test channel and wait for new messages.

NOTE: you can publish to test channel in redis console (`redis-cli`) as below:

127.0.0.1:6379> publish test hello_python
(integer) 1

Usecases are endless! You can use as part of messaging infrastructure in your microservice environment or as a chat system, you name it! :)

#python #redis #pubsub #publish #subscribe #thread
Executing MySQL statements from a text file:

In MySQL it is possible to put your SQL statements in a file and then tell mysql to read its input from that file. To do so, create a text file text_file that contains the statements you wish to execute. Then invoke mysql as shown here:

shell> mysql db_name < text_file

NOTE: If you place a USE db_name statement as the first statement in the file, it is unnecessary to specify the database name on the command line.


If you are already running mysql, you can execute an SQL script file using the source command or \. command:

mysql> source file_name


#mysql #execute #command #statement
How to add color to your logs in python?

It's easy as pie, just install coloredlogs with pip and then:

import coloredlogs, logging
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG')

# Some examples.
logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")

By default the install() function installs a handler on the root logger, this means that log messages from your code and log messages from the libraries that you use will all show up on the terminal.

If you don't want to see log messages from libraries, you can pass a specific logger object to the install() function. In this case only log messages originating from that logger will show up on the terminal:

coloredlogs.install(level='DEBUG', logger=logger)

#log #logger #coloredlogs #logging #color