| 372 | {{{#!th style="background: #ddd" |
| 373 | '''Label''' |
| 374 | }}} |
| 375 | {{{#!th style="background: #ddd" |
| 376 | '''Python 2''' |
| 377 | }}} |
| 378 | {{{#!th style="background: #ddd" |
| 379 | '''Python 3''' |
| 380 | }}} |
| 381 | {{{#!th style="background: #ddd" |
| 382 | '''Python 2 and 3 compatible solution''' |
| 383 | }}} |
| 384 | |----------------------- |
| 385 | {{{#!td |
| 386 | Strings - |
| 387 | bytes/str/unicode |
| 388 | }}} |
| 389 | {{{#!td |
| 390 | }}} |
| 391 | {{{#!td |
| 392 | }}} |
| 393 | {{{#!td |
| 394 | {{{ |
| 395 | Use decode and encode functions from grass.script.utils |
| 396 | }}} |
| 397 | }}} |
| 398 | |----------------------- |
| 399 | {{{#!td |
| 400 | String functions |
| 401 | }}} |
| 402 | {{{#!td |
| 403 | {{{ |
| 404 | String.split |
| 405 | String.join |
| 406 | }}} |
| 407 | }}} |
| 408 | {{{#!td |
| 409 | {{{ |
| 410 | "".split(), "".join(" ") |
| 411 | }}} |
| 412 | }}} |
| 413 | {{{#!td |
| 414 | Use functions as in Python 3 |
| 415 | }}} |
| 416 | |----------------------- |
| 417 | {{{#!td |
| 418 | String types |
| 419 | }}} |
| 420 | {{{#!td |
| 421 | {{{ |
| 422 | Types.StringTypes |
| 423 | Types.Stringtype |
| 424 | }}} |
| 425 | }}} |
| 426 | {{{#!td |
| 427 | {{{ |
| 428 | str or unicode |
| 429 | str |
| 430 | }}} |
| 431 | }}} |
| 432 | {{{#!td |
| 433 | Use direct types like str, unicode, bytes as: |
| 434 | {{{ |
| 435 | if sys.version_info.major == 3: |
| 436 | unicode = str |
| 437 | else: |
| 438 | bytes = str |
| 439 | }}} |
| 440 | }}} |
| 441 | |----------------------- |
| 442 | {{{#!td |
| 443 | ps.communicate(): |
| 444 | stdout, stderror |
| 445 | }}} |
| 446 | {{{#!td |
| 447 | }}} |
| 448 | {{{#!td |
| 449 | }}} |
| 450 | {{{#!td |
| 451 | Use decode: |
| 452 | {{{ |
| 453 | from grass.script.utils import decode |
| 454 | |
| 455 | stdout = decode(stdout) |
| 456 | stderr = decode(stdout) |
| 457 | }}} |
| 458 | }}} |
| 459 | |----------------------- |
| 460 | {{{#!td |
| 461 | Opening files |
| 462 | }}} |
| 463 | {{{#!td |
| 464 | {{{ |
| 465 | fileName = file(“example.txt”, w) |
| 466 | }}} |
| 467 | }}} |
| 468 | {{{#!td |
| 469 | {{{ |
| 470 | fileName = open(“example.txt”, w) |
| 471 | }}} |
| 472 | }}} |
| 473 | {{{#!td |
| 474 | Replace file() with open() to open files |
| 475 | }}} |
| 476 | |----------------------- |
| 477 | {{{#!td |
| 478 | Filter function |
| 479 | }}} |
| 480 | {{{#!td |
| 481 | {{{ |
| 482 | filter(a_function, a_sequence) |
| 483 | }}} |
| 484 | Filter returns a list |
| 485 | }}} |
| 486 | {{{#!td |
| 487 | {{{ |
| 488 | filter(a_function, a_sequence) |
| 489 | }}} |
| 490 | Filter returns an iterator, not a list |
| 491 | }}} |
| 492 | {{{#!td |
| 493 | Explicit conversion to list using: |
| 494 | {{{ |
| 495 | list( filter(a_function, a_sequence) ) |
| 496 | }}} |
| 497 | }}} |
| 498 | |----------------------- |
| 499 | {{{#!td |
| 500 | Urlopen proxies |
| 501 | }}} |
| 502 | {{{#!td |
| 503 | }}} |
| 504 | {{{#!td |
| 505 | }}} |
| 506 | {{{#!td |
| 507 | Create proxy handler: |
| 508 | https://docs.python.org/3.0/library/urllib.request.html#urllib.request.ProxyHandler |
| 509 | |
| 510 | Read more: |
| 511 | http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#urllib |
| 512 | }}} |
| 513 | |----------------------- |
| 514 | {{{#!td |
| 515 | Import errors |
| 516 | }}} |
| 517 | {{{#!td |
| 518 | }}} |
| 519 | {{{#!td |
| 520 | }}} |
| 521 | {{{#!td |
| 522 | Use try/except to catch Import errors and deal accordingly |
| 523 | }}} |
| 524 | |----------------------- |
| 525 | {{{#!td |
| 526 | Relative Imports |
| 527 | }}} |
| 528 | {{{#!td |
| 529 | {{{ |
| 530 | import example |
| 531 | from x import y |
| 532 | }}} |
| 533 | }}} |
| 534 | {{{#!td |
| 535 | {{{ |
| 536 | from . import example |
| 537 | from .x import y |
| 538 | }}} |
| 539 | }}} |
| 540 | {{{#!td |
| 541 | Use period (.) in import calls |
| 542 | }}} |
| 543 | |----------------------- |
| 544 | {{{#!td |
| 545 | Dictionary methods |
| 546 | }}} |
| 547 | {{{#!td |
| 548 | {{{ |
| 549 | a_dictionary.keys() |
| 550 | a_dictionary.items() |
| 551 | a_dictionary.iterkeys() |
| 552 | }}} |
| 553 | }}} |
| 554 | {{{#!td |
| 555 | {{{ |
| 556 | list(a_dictionary.keys()) |
| 557 | list(a_dictionary.items()) |
| 558 | iter(a_dictionary.keys()) |
| 559 | }}} |
| 560 | }}} |
| 561 | {{{#!td |
| 562 | Read more: |
| 563 | http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#dict |
| 564 | }}} |
| 565 | |----------------------- |
| 566 | {{{#!td |
| 567 | Other dictionary iterators |
| 568 | }}} |
| 569 | {{{#!td |
| 570 | {{{ |
| 571 | a_dictionary.iterkeys() |
| 572 | a_dictionary.iteritems() |
| 573 | a_dictionary.itervalues() |
| 574 | }}} |
| 575 | }}} |
| 576 | {{{#!td |
| 577 | {{{ |
| 578 | six.iterkeys(a_dictionary) |
| 579 | six.iteritems(a_dictionary) |
| 580 | six.itervalues(a_dictionary) |
| 581 | }}} |
| 582 | }}} |
| 583 | {{{#!td |
| 584 | Use function from six library |
| 585 | }}} |
| 586 | |----------------------- |
| 587 | {{{#!td |
| 588 | Map list |
| 589 | }}} |
| 590 | {{{#!td |
| 591 | {{{ |
| 592 | map(x, y) |
| 593 | }}} |
| 594 | Returns list |
| 595 | }}} |
| 596 | {{{#!td |
| 597 | {{{ |
| 598 | map(x,y) |
| 599 | }}} |
| 600 | Returns iterator |
| 601 | }}} |
| 602 | {{{#!td |
| 603 | {{{ |
| 604 | list(map(x,y)) |
| 605 | }}} |
| 606 | Use list to wrap around map |
| 607 | }}} |
| 608 | |