Skip to content

Commit 1228580

Browse files
authored
Add syntax highlight
I add syntax highlighting to all examples. I cared to always choose the right one (Lua/Python)
1 parent 36a4da2 commit 1228580

1 file changed

Lines changed: 66 additions & 42 deletions

File tree

README.md

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Examples
3636
Lua inside Python
3737
A basic example.
3838

39-
```
39+
```lua
4040
>>> import lua
4141
>>> lg = lua.globals()
4242
>>> lg.string
@@ -49,7 +49,7 @@ A basic example.
4949

5050
Now, let's put a local object into Lua space.
5151

52-
```
52+
```lua
5353
>>> d = {}
5454
>>> lg.d = d
5555
>>> lua.execute("d['key'] = 'value'")
@@ -65,14 +65,14 @@ Good!
6565

6666
Is the python interface available inside the Lua interpreter?
6767

68-
```
68+
```lua
6969
>>> lua.eval("python")
7070
<Lua table at 0x81c7540>
7171
```
7272

7373
Yes, it looks so. Let's nest some evaluations and see a local reference passing through.
7474

75-
```
75+
```python
7676
>>> class MyClass: pass
7777
...
7878
>>> obj = MyClass()
@@ -84,15 +84,15 @@ Yes, it looks so. Let's nest some evaluations and see a local reference passing
8484

8585
Are you still following me? Good. Then you've probably noticed that the Python interpreter state inside the Lua interpreter state is the same as the outside Python we're running. Let's see that in a more comfortable way.
8686

87-
```
87+
```python
8888
>>> lua.execute("pg = python.globals()")
8989
>>> lua.eval("pg.obj")
9090
<__main__.MyClass instance at 0x403ccb4c>
9191
```
9292

9393
Things get more interesting when we start to really mix Lua and Python code.
9494

95-
```
95+
```python
9696
>>> table = lua.eval("table")
9797
>>> def show(key, value):
9898
... print "key is %s and value is %s" % (`key`, `value`)
@@ -107,7 +107,7 @@ key is 'b' and value is 2
107107

108108
Of course, in this case the same could be achieved easily with Python.
109109

110-
```
110+
```python
111111
>>> def show(key, value):
112112
... print "key is %s and value is %s" % (`key`, `value`)
113113
...
@@ -125,7 +125,7 @@ Python inside Lua
125125

126126
Now, let's have a look from another perspective. The basic idea is exactly the same.
127127

128-
```
128+
```python
129129
> require("python")
130130
> python.execute("import string")
131131
> pg = python.globals()
@@ -137,7 +137,7 @@ hello world!
137137

138138
As Lua is mainly an embedding language, getting access to the batteries included in Python may be interesting.
139139

140-
```
140+
```python
141141
> re = python.import("re")
142142
> pattern = re.compile("^Hel(lo) world!")
143143
> match = pattern.match("Hello world!")
@@ -147,7 +147,7 @@ lo
147147

148148
Just like in the Python example, let's put a local object in Python space.
149149

150-
```
150+
```python
151151
> d = {}
152152
> pg.d = d
153153
> python.execute("d['key'] = 'value'")
@@ -157,22 +157,22 @@ key value
157157

158158
Again, let's grab back the reference from Python space.
159159

160-
```
160+
```python
161161
> d2 = python.eval("d")
162162
> print(d == d2)
163163
true
164164
```
165165

166166
Is the Lua interface available to Python?
167167

168-
```
168+
```pythom
169169
> =python.eval("lua")
170170
<module 'lua' (built-in)>
171171
```
172172

173173
Good. So let's do the nested trick in Lua as well.
174174

175-
```
175+
```python
176176
> t = {}
177177
> =t
178178
table: 0x80fbdb8
@@ -183,15 +183,15 @@ table: 0x80fbdb8
183183

184184
It means that the Lua interpreter state inside the Python interpreter is the same as the outside Lua interpreter state. Let's show that in a more obvious way.
185185

186-
```
186+
```lua
187187
> python.execute("lg = lua.globals()")
188188
> =python.eval("lg.t")
189189
table: 0x80fbdb8
190190
```
191191

192192
Now for the mixing example.
193193

194-
```
194+
```lua
195195
> function notthree(num)
196196
>> return (num ~= 3)
197197
>> end
@@ -218,7 +218,7 @@ Attribute vs. Subscript object access
218218

219219
Accessing an attribute or using the subscript operator in Lua give access to the same information. This behavior is reflected in the Python special object that encapsulates Lua objects, allowing Lua tables to be accessed in a more comfortable way, and also giving access to objects which use protected Python keywords (such as the print function). For example:
220220

221-
```
221+
```python
222222
>>> string = lua.eval("string")
223223
>>> string.lower
224224
<Lua function at 0x81c6bf8>
@@ -228,7 +228,7 @@ Accessing an attribute or using the subscript operator in Lua give access to the
228228

229229
Using Python from the Lua side requires a little bit more attention, since Python has a more strict syntax than Lua. The later makes no distinction between attribute and subscript access, so we need some way to know what kind of access is desired at a given moment. This control is provided using two functions: `asindx()` and `asattr()`. These functions receive a single Python object as parameter, and return the same object with the given access discipline. Notice that dictionaries and lists use the index discipline by default, while other objects use the attribute discipline. For example:
230230

231-
```
231+
```python
232232
> dict = python.eval("{}")
233233
> =dict.keys
234234
nil
@@ -251,20 +251,22 @@ Lua inside Python
251251
When executing Python as the host language, the Lua functionality is accessed by importing the lua module. When Lua is the host language, the lua module will already be available in the global Python scope.
252252
Below is a description of the functions available in the lua module.
253253

254-
`lua.execute(statement)`
254+
```python
255+
lua.execute(statement)
256+
```
255257

256258
This function will execute the given statement inside the Lua interpreter state.
257259
Examples:
258260

259-
```
261+
```lua
260262
>>> lua.execute("foo = 'bar'")
261263
lua.eval(expression)
262264
```
263265

264266
This function will evaluate the given expression inside the Lua interpreter state, and return the result. It may be used to acquire any object from the Lua interpreter state.
265267
Examples:
266268

267-
```
269+
```lua
268270
>>> lua.eval("'foo'..2")
269271
'foo2'
270272
>>> lua.eval('string')
@@ -274,13 +276,15 @@ Examples:
274276
'hello world!'
275277
```
276278

277-
`lua.globals()`
279+
```lua
280+
lua.globals()
281+
```
278282

279283
Return the Lua global scope from the interpreter state.
280284

281285
Examples:
282286

283-
```
287+
```lua
284288
>>> lg = lua.globals()
285289
>>> lg.string.lower("Hello world!")
286290
'hello world!'
@@ -292,12 +296,14 @@ Examples:
292296
Hello world!
293297
```
294298

295-
`lua.require(name)`
299+
```lua
300+
lua.require(name)
301+
```
296302

297303
Executes the require() Lua function, importing the given module.
298304

299305
Examples:
300-
```
306+
```lua
301307
>>> lua.require("testmod")
302308
True
303309
>>> lua.execute("func()")
@@ -312,22 +318,26 @@ Unlike Python, Lua has no default path to its modules. Thus, the default path of
312318
Unfortunately, there's a minor inconvenience for our purposes regarding the Lua system which imports external shared objects. The hardcoded behavior of the loadlib() function is to load shared objects without exporting their symbols. This is usually not a problem in the Lua world, but we're going a little beyond their usual requirements here. We're loading the Python interpreter as a shared object, and the Python interpreter may load its own external modules which are compiled as shared objects as well, and these will want to link back to the symbols in the Python interpreter. Luckily, fixing this problem is easier than explaining the problem. It's just a matter of replacing the flag RTLD_NOW in the loadlib.c file of the Lua distribution by the or'ed version RTLD_NOW|RTLD_GLOBAL. This will avoid "undefined symbol" errors which could eventually happen.
313319
Below is a description of the functions available in the python module.
314320

315-
`python.execute(statement)`
321+
```python
322+
python.execute(statement)
323+
```
316324

317325
This function will execute the given statement inside the Python interpreter state.
318326
Examples:
319327

320-
```
328+
```python
321329
> python.execute("foo = 'bar'")
322330
```
323331

324-
`python.eval(expression)`
332+
```python
333+
python.eval(expression)
334+
```
325335

326336
This function will evaluate the given expression inside the Python interpreter state, and return the result. It may be used to acquire any object from the Python interpreter state.
327337

328338
Examples:
329339

330-
```
340+
```python
331341
> python.execute("import string")
332342
> =python.eval("string")
333343
<module 'string' from '/usr/lib/python2.3/string.pyc'>
@@ -336,12 +346,14 @@ Examples:
336346
hello world!
337347
```
338348

339-
`python.globals()`
349+
```python
350+
python.globals()
351+
```
340352

341353
Return the Python global scope dictionary from the interpreter state.
342354
Examples:
343355

344-
```
356+
```python
345357
> python.execute("import string")
346358
> pg = python.globals()
347359
> =pg.string.lower("Hello world!")
@@ -350,12 +362,14 @@ hello world!
350362
hello world!
351363
```
352364

353-
`python.locals()`
365+
```python
366+
python.locals()
367+
```
354368

355369
Return the Python local scope dictionary from the interpreter state.
356370
Examples:
357371

358-
```
372+
```python
359373
> function luafunc()
360374
>> print(python.globals().var)
361375
>> print(python.locals().var)
@@ -366,34 +380,40 @@ nil
366380
value
367381
```
368382

369-
`python.builtins()`
383+
```python
384+
python.builtins()
385+
```
370386

371387
Return the Python builtins module dictionary from the interpreter state.
372388
Examples:
373389

374-
```
390+
```python
375391
> pb = python.builtins()
376392
> =pb.len("Hello world!")
377393
12
378394
```
379395

380-
`python.import(name)`
396+
```python
397+
python.import(name)
398+
```
381399

382400
Imports and returns the given Python module.
383401
Examples:
384402

385-
```
403+
```python
386404
> os = python.import("os")
387405
> =os.getcwd()
388406
/home/niemeyer/src/lunatic-python
389407
```
390408

391-
`python.asattr(pyobj)`
409+
```python
410+
python.asattr(pyobj)
411+
```
392412

393413
Return a copy of the given Python object with an attribute access discipline.
394414
Examples:
395415

396-
```
416+
```python
397417
> dict = python.eval("{}")
398418
> =dict.keys
399419
nil
@@ -410,12 +430,14 @@ function: 0x80fa9b8
410430
['keys']
411431
```
412432

413-
`python.asindx(pyobj)`
433+
```python
434+
python.asindx(pyobj)
435+
```
414436

415437
Return a copy of the given Python object with an index access discipline.
416438
Examples:
417439

418-
```
440+
```python
419441
> buffer = python.eval("buffer('foobar')")
420442
> =buffer[0]
421443
stdin:1: unknown attribute in python object
@@ -428,12 +450,14 @@ stack traceback:
428450
f
429451
```
430452

431-
`python.asfunc(pyobj)`
453+
```python
454+
python.asfunc(pyobj)
455+
```
432456

433457
Return a copy of the given Python object enclosed in a Lua function closure. This is useful to use Python callable instances in places that require a Lua function. Python methods and functions are automatically converted to Lua functions, and don't require to be explicitly converted.
434458
Examples:
435459

436-
```
460+
```python
437461
> python.execute("class Join:\n def __call__(self, *args):\n return '-'.join(args)")
438462
> join = python.eval("Join()")
439463
> =join

0 commit comments

Comments
 (0)