Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Any time scientists disagree, it's because we have insufficient data. Then we can agree on what kind of data to get; we get the data; and the data solves the problem. Either I'm right or you're right, or we're both wrong. And we move on.

Neil deGrasse Tyson

#kafka #ebook
How to ignore extra fields for schema validation in Mongoengine?

Some records currently have extra fields that are not included in my model schema (by error, but I want to handle these cases). When I try to query the DB and transform the records into the schema, I get the following error:

FieldDoesNotExist
The field 'X' does not exist on the document 'Y'



For ignoring this error when having extra fields while getting data, set strict to False in your meta dictionary.


class User(Document):
email = StringField(required=True, unique=True)
password = StringField()
meta = {'strict': False}



#mongodb #mongo #python #mongoengine #strict #FieldDoesNotExist
In MongoDB you can remove duplicate documents based on a specific field:

db.yourCollection.aggregate([
{ "$group": {
"_id": { "yourDuplicateKey": "$yourDuplicateKey" },
"dups": { "$push": "$_id" },
"count": { "$sum": 1 }
}},
{ "$match": { "count": { "$gt": 1 } }}
]).forEach(function(doc) {
doc.dups.shift();
db.yourCollection.remove({ "_id": {"$in": doc.dups }});
});

It uses aggregation to group by based on the given key then add its _id into dups field and its count in count field. It will project fields with count of more than 1 using $match. At the end loops over each document and remove all duplicate fields except the first one (`shift` will cause this behaviour).

#mongodb #mongo #duplicates #duplication
How to sort data based on a column in Pandas?

You can use sort_values in order to sort data in a dataframe:

df.sort_values(['credit'], ascending=False, inplace=True)

The above sample assumes that you have a data frame called df and sort it based on user credit. The sort order is Descending (ascending=False). and it sorts in place (You don't have to copy the result into a new dataframe).

#python #pandas #dataframe #sort #inplace
How do you query is not null in MongoDB?

db.mycollection.find({ 'fieldname' : { $ne: null } });

#database #mongodb #query #ne #null #not_null
Forwarded from Alireza Hos.
امشب شب ولادت سادات عالم است/ امشب شب عروسی زهرا و حیدر است

امشب به عرش زمزمه شادی علی است/ امشب شب مبارک دامادی علی است

بنت اسد که بوده ملک دست بوس تو/ عیدی بده که فاطمه گشته عروس تو
How to prepend a string to all file names in a directory in a bash script?

for f in *.py; do mv "$f" "old-$f"; done

The above one-line will loop over all files in current directory with .py extension and prepend old- into the files.

So for example a file with name main.py will be renamed to old-main.py

#python #bash #script #prepend #move #rename #for
How do you know if a remote web server is Linux based or Windows based? (Ignore this if there is pfsense/ha proxy in front)

ping the remote ip:

ping iranhost.com

The output is similar to:

PING iranhost.com (174.142.214.47): 56 data bytes
64 bytes from 174.142.214.47: icmp_seq=0 ttl=105 time=174.379 ms
64 bytes from 174.142.214.47: icmp_seq=1 ttl=105 time=180.315 ms
64 bytes from 174.142.214.47: icmp_seq=2 ttl=105 time=177.937 ms


Look at the ttl part of the output. If it's above 100, it is a windows based machine otherwise it is a Linux based machine.


#os #linux #windows #ping #ttl #iranhost
Forwarded from Alireza Hos.
This media is not supported in your browser
VIEW IN TELEGRAM
🔺حمایت کم نظیر مردم ترکیه از پول کشورشان

🔹کاهش 13درصدی ارزش دلار نسبت به لیر از زمان شروع کمپین مردمی
Did you know that the below code returns True?

>>> bool("False")
True

That's because False is a string and boolean of non-empty string is True. There are workarounds:

1- s == 'True'

Do not use this piece of code if you don't know what you're doing. Misusing this technic puts a mess in your code! Be cautious.

2- The other more elegant way is to use json library like below:

>>> import json
>>> json.loads("false".lower())
False
>>> json.loads("True".lower())
True

NOTE: you need to make it lower case in order to turn the string into a boolean value.

#python #boolean #true #false #cast #convert #bool
How to clone a database in MySQL?

mysqldump -u root db_name | mysql -u root new_db_name

NOTE: if it gets password provide -p to both commands.

#mysql #clone #copy #database #copy_database
I_Heart_Logs.pdf
4.7 MB