Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Python-Projects/Metaprogramming.py at master · RegaipKURT/Python-Projects · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
RegaipKURT
Python-Projects
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python-Projects
/
Metaprogramming.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
147 lines (115 loc) · 5.9 KB
master
Breadcrumbs
Python-Projects
/
Metaprogramming.py
Copy path
Top
File metadata and controls
Code
Blame
147 lines (115 loc) · 5.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Metaprogramlama birçok programlama dilinde bulunur.
Metaprogramlama en basit haliyle bir tür kod denetleme aracıdır.
Yazdığımız kodun içeriğini program çalışırken içerden veya dışardan
değiştirmek veya denetlemek istediğimizde bu yapıyı kullanırız.
Bu programı çalıştırmak için bulunduğunuz dizinde deneme.py ve bos_dosya.py isimli
iki adet python dosyası oluşturun. Daha sonra bu python programlarını bu dosya üzerinden programlayalım.
deneme.py içeriği şöyle olsun:
<--
def selamla(*args, **kwargs):
print("Hoşgeldiniz", kwargs["isim"], kwargs["soyad"] + "!")
a = "Kemal"
def w():
global a
import os
os.system("whoami")
print(a)
if __name__ == '__main__':
selamla(isim="Regaip", soyad="Kurt")
-->
"""
with
open
(
"bos_dosya.py"
,
"a"
)
as
f
:
f
.
close
()
#oluşturduğumuz dosyaların import edilmesi
import
deneme
import
bos_dosya
import
Metaprogramming
# programın kendisini import etmesi
if
__name__
==
'__main__'
:
#Yayıncı isminde bir değişkenim var. En altta nasıl olduğunu görebilirsin.
print
(
"Yayıncı:"
,
Metaprogramming
.
Yayinci
)
#print(deneme.__dict__) # deneme dosyası içindeki tüm özellikler ve değişkenler
# metaclass aracılığıyla oluşturacağımız insan sınıfı için yapıcı metod
def
insan_init
(
self
,
boy
,
kilo
,
isim
):
self
.
kilo
=
kilo
self
.
boy
=
boy
self
.
isim
=
isim
# type fonksiyonuyla insan sınıfı oluşturma
insan
=
type
(
"insan"
,
#sınıfın adı
(),
#base class tanımlama (biz kullanmayacağız)
# sınıf metodlarımızı tanımlama
{
"__init__"
:
insan_init
,
"get_name"
:
lambda
self
:
print
(
self
.
isim
),
"VKI"
:
lambda
self
:
print
(
self
.
isim
+
" kişisinin Vücut/Kitle İndeksi: "
+
"{:.2f}"
.
format
(
self
.
kilo
/
((
self
.
boy
/
100
)
**
2
)))
}
)
#deneme dosyasının gömülü metodlarına bakalım
print
(
dir
(
deneme
))
#henüz biz hiç bi şey tanımlamadık
# deneme dosyasında yeni_nesne adında bir insan sınıfı oluşturalım
deneme
.
__dict__
[
"yeni_nesne"
]
=
insan
(
175
,
85
,
"Değişik insan"
)
deneme
.
yeni_nesne
.
get_name
()
# isim alma
deneme
.
yeni_nesne
.
VKI
()
# vücut kitle indeksi alma
print
(
"Orjinal değer: "
,
deneme
.
a
)
#deneme dosyası içinde tanımlanmış a değişkeninin değerini aldık
deneme
.
a
=
"Atatürk"
print
(
"Değiştirdiğimiz değer: "
,
deneme
.
a
)
print
(
dir
(
deneme
))
# deneme dosyasının gömülü metodlarında artık bizim oluşturduğumuz nesne görünecek.
# print(deneme.__dict__["__file__"].split("\\")[-1])
# print(deneme.__dict__["__cached__"])
# bos_dosya.py isimli programda yeni listeler tanımlayalım
# liste+number olacak şekilde 100 tane liste tanımlayalım ve
# içine o listenin sonundaki numaraya kadar olan sayıları ekleyelim
# for i in range(100):
# bos_dosya.__dict__["liste" + str(i)] = [j for j in range(i)]
# #bos_dosya.py içinde 20 ye kadar olan listelere bakalım
# for i in range(20):
# print(bos_dosya.__dict__["liste"+str(i)])
# METAPROGRAMMING YÖNTEMLERİYLE HİÇBİR İZNİMİZ OLMAYAN BİR PYTHON DOSYASINA DA ERİŞİP
# İÇİNDEKİLERİ OKUYABİLİR VEYA ÇALIŞTIRABİLİRİZ. BUNUN İÇİN __pycache__ dosyasının oluşturulmuş olması gerekir.
#deneme.py dosyasının izinlerini değiştirip deneyebilirsiniz!
# __pycache__ içideki dosyayı okuma izniniz olmalıdır!
import
deneme
#okuyamasak bile bu python dosyasını cache üzerinden import edebilecektir. Çünkü cache okunabilir durumda!
def
hello
():
print
(
"HACKED!"
)
deneme
.
__dict__
[
"hello"
]
=
hello
deneme
.
hello
()
# DOSYA İÇİNDEN SELAMLA VE W İSİMLİ FONKSİYONLARI ÇALIŞTIRALIM
# deneme.selamla(isim="Regaip", soyad="Kurt")
# deneme.w()
# #DOSYANIN İÇİNDEKİLERİ CACHE'I OKUDUĞUMUZ İÇİN GÖREBİLDİK AMA AÇMAYA ÇALIŞTIĞIMIZDA HATA VERECEKTİR.
# with open("deneme.py", "r") as f:
# print(f.readlines())
# METAPROGRAMLAMANIN KULLANIMI
# Metaprogramlamada metaclasslar kullanılır ve bir metaclass type dan kalıtım alır.
# metaclass alan bir sınıfın özellikleri metaclass tarafından düzenlenebilir. Örnek verelim:
class
StringMeta
(
type
):
# bureda eğer bu metaclass ile oluşturulan sınıfın adı y ile başlıyorsa
# o sınıfa sınıfın adını yazdıran bir yaz fonksiyonu eklensin
def
__new__
(
cls
,
what
,
bases
=
None
,
dict
=
None
):
print
(
dict
)
# dict sınıfın özelliklerini barındırır.
new_dict
=
{}
if
dict
[
"__qualname__"
].
startswith
(
"y"
):
print
(
"Sınıf adı y ile başlıyor, yaz fonksiyonu ayarlanacak!"
)
def
yaz
(
self
):
print
(
dict
[
"__qualname__"
])
#Soru: Bu ne işe yarar? Diyebiiliriz.
#Cevap: Örneğin bir framework yazarken sınıfları denetleme ve düzenleme ihtiyacı oluşacaktır.
# Böylece belirli sınıflar üzerinde işlem yapabilme olanağı sağlanmış olur.
new_dict
[
"yaz"
]
=
yaz
return
type
.
__new__
(
cls
,
what
,
bases
,
new_dict
)
class
yreg
(
metaclass
=
StringMeta
):
pass
class
reg
(
metaclass
=
StringMeta
):
pass
#yreg sınıfının adı y ile başladığı için yaz metodu olacak içinde.
y
=
yreg
()
y
.
yaz
()
r
=
reg
()
# r.yaz çalışmaz çünkü r nesnesinin oluşturulduğu sınıfın adı y ile başlamıyor, yaz metodu tanımlanmadı!
# dolayısıyla yaz fonksiyonuna sahip değil!
#r.yaz()
#Program çalışma zamanı içinde kendini de değiştirebilir!
Metaprogramming
.
__dict__
[
"Yayinci"
]
=
"Regaip KURT"
#program import edildiğinde Kemal değeri yazılırken en sonda Atatürk değeri yazacak.
eval
(
"deneme.w()"
)
exec
(
"deneme.w()"
)
You can’t perform that action at this time.